Click here to Skip to main content
15,886,723 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get the supplier id of the supplier name (combo box value) to appear in a textbox... can this code work?

C#
string query = "select * from Supplier WHERE SupplierName = '" + cbosupplier.SelectedValue.ToString() + "'";
              SqlCommand cmd = new SqlCommand(query, cn);
              cmd.CommandText = query;

              txtsupplierno.Text = query;
Posted
Updated 27-Jun-12 3:56am
v3

No.

Next question?

You have to execute the query, and use the resulting dataset - Oh, and please use parameterised queries. It help avoid accidental or deliberay damage to your database...
Try this:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Id FROM Supplier WHERE SupplierName = @SN", con))
        {
        cmd.Parameters.AddWithValue("@SN", cbosupplier.SelectedValue);
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                }
            }
        }
    }
 
Share this answer
 
You are going to need to execute a reader, read the query results, and save the results into your property. I would look something like this:

C#
SqlDataReader rdr = cmd.ExecuteReader();

while (rdr.Read())
{
   txtsupplierno.Text =rdr["SupplierId"].ToString();
}


This would loop through each record and put that value into the text field, overwriting the last value. It isn't ideal if you are going to return more than one row but it gets the job done (quick and dirty first pass).

Next you need to think about protecting yourself against SQL injection. You should never pass a value from the UI straight into a SQL statement. The user could put code in the text box that would give them control over your database. If your combobox does not allow values to be typed in, you are safer, but I still like to see values to be inserted into statements using parameters.

Here is a good tutorial with more information that should help you out:

http://www.csharp-station.com/Tutorial/AdoDotNet/lesson03[^]
 
Share this answer
 
Comments
wolfsor 27-Jun-12 11:21am    
This code works after selecting an item in the combo box, but when I change a selected item, I get an error
wolfsor 27-Jun-12 11:22am    
do you know how to fix that?
Tim Corey 27-Jun-12 12:07pm    
You are going to need to step through the code. My guess would be that there is a logic error somewhere in your code.
No you use datareader or datatable
 
Share this answer
 
This is your Code to Find SupplierID.

C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConName"].ConnectionString);
        SqlDataAdapter adp = new SqlDataAdapter("Select SupplierID from Suppliers where SupplierName = '" + cbosupplier.SelectedValue.ToString() + "'", con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        string SuppID = ds.Tables[0].Rows[0][0].ToString();


Mark as Answer if it helps you.
 
Share this answer
 
hi if i'm not wrong than you must be having this drop down which displays list of suppliers and you want to display its corresponding Id.

so to do this you should Bind the dropdown display text with the name of suppliers
and bind dropdowns value member with the id of suppliers.
it is possible to have two different fields binded with the same dropdown

after this there is no need to execute select command
you can to so as following
txtsupplierno.Text = cbosupplier.SelectedValue;

while cbosupplier.SelectedText will display the suppliers name...

thanks
hope the suggestion helps you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900