Hey everyone,

I am in the midst of creating a very basic auction site using C#. Only been learning the language for 3 months so still very much getting used to it and finding out new things everyday.

I am using a local database to store my data and want to include a search option on the windows form that will allow a user to search for a model of laptop. Example - The user can type Sony and the Sony laptop I have stored will show.

I was wondering if someone could explain this code to me:

private void LaptopsForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'laptopsDataSet.Laptop_Information' table. You can move, or remove it, as needed.
            this.laptop_InformationTableAdapter.Fill(this.laptopsDataSet.Laptop_Information);

            
            
            List<string> manufactures = new List<string>();
            for (int i = 0; i < laptopsDataSet.Laptop_Information.Count; i++)
            {
                manufactures.Add(laptopsDataSet.Laptop_Information.ElementAt(i).Manufacturer);
            }

            int index = 0;
            foreach (string s in manufactures)
            {

                if (s.Equals("Sony"))
                {
                    label1.Text = laptopsDataSet.Laptop_Information.ElementAt(index).Model;
                    break;
                }
                else
                    index++;
            }

Thanks guys!

Hello, jboy1803.
Here we go ..

this.laptop_InformationTableAdapter.Fill(this.laptopsDataSet.Laptop_Information);

Here, you make your adapter to obtain the data from your DataBase and put it in the specified table. You can treat adapter as a communication channel between your DataBase and the DataSet/DataTable in your App.

List<string> manufactures = new List<string>();
for (int i = 0; i < laptopsDataSet.Laptop_Information.Count; i++)
{
manufactures.Add(laptopsDataSet.Laptop_Information.ElementAt(i).Manufacturer);
}

Here you iterate through the result table and extract ALL manufacturers into a Collection.

int index = 0;
foreach (string s in manufactures)
{

if (s.Equals("Sony"))
{
label1.Text = laptopsDataSet.Laptop_Information.ElementAt(index).Model;
break;
}
else
index++;
}

Here you iterate through the result collection and search for a Sony manufacturer. If you found one - put it in the Label.

That's pretty much all :)

Thanks for your reply.

Is there any way I could get the "s" string to be input by the user themselves without having to call it myself on my code?

Example - A text box which when a user types in "Sony" displays the model?

Thanks again!

Sure, you can.
You get the text of a label:

label1.Text

in the same way you can get the text of your textbox.

Thanks again.

Ok, managed to get the search working but it only keeps showing the same laptop. I search for Sony and I get Samsung and I search for Samsung and I get Samsung.

Can anyone help me out?

private void button1_Click(object sender, EventArgs e)
            
       {
            List<string> manufactures = new List<string>();
            for (int i = 0; i < laptopsDataSet.Laptop_Information.Count; i++)
            {
                manufactures.Add(laptopsDataSet.Laptop_Information.ElementAt(i).Manufacturer);
            }

            List<string> item = new List<string>();
            for (int i = 0; i < laptopsDataSet.Laptop_Information.Count; i++)
            {
                item.Add(laptopsDataSet.Laptop_Information.ElementAt(i).Item_Description);
            }

            List<decimal> currentp = new List<decimal>();
            for (int i = 0; i < laptopsDataSet.Laptop_Information.Count; i++)
            {
                currentp.Add(laptopsDataSet.Laptop_Information.ElementAt(i).Current_Price);
            }



            int index = 0;
            foreach (string s in manufactures)
            {

                if (textBox1.Text == ("Sony"))
                {
                    label1.Text = laptopsDataSet.Laptop_Information.ElementAt(index).Model;
                    label2.Text = laptopsDataSet.Laptop_Information.ElementAt(index).Item_Description;
                    label3.Text = Convert.ToString(laptopsDataSet.Laptop_Information.ElementAt(index).Current_Price);
                    break;
                    index++;
                }
                else if (textBox1.Text == ("Samsung"))
                {
                    label1.Text = laptopsDataSet.Laptop_Information.ElementAt(index).Model;
                    label2.Text = laptopsDataSet.Laptop_Information.ElementAt(index).Item_Description;
                    label3.Text = Convert.ToString(laptopsDataSet.Laptop_Information.ElementAt(index).Current_Price);
                    break;
                }

                 else   index++;
            }
        }

Thanks

Thanks again!

The problem I now have is that if I type 'Sony' it displays the results but if I type 'sony' nothing happens. Is there a method for a string like an 'or' or a way I can include multiple keywords in a search?

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.