Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have windows form.



I want to get a value like "1B" to a textbox from the sql table name as productT.

here is my code but it wrong i think
C#
private void button5_Click(object sender, EventArgs e)
       {
           String commandstring;
           commandstring = "SELECT * FROM productT  WHERE spiceID='" + Convert.ToInt32(textBox1.Text) + "'";
           SqlDataReader dr1 = null;
           SqlCommand com2 = new SqlCommand(commandstring, conn1);
           try
           {
               conn1.Open();
           }
           catch
           {
               MessageBox.Show("error in seaching", "message", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
           }
           dr1 = com2.ExecuteReader();


           if (dr1.Read())
           {
               comboBox1.Text = dr1[1].ToString();
               dateTimePicker1.Value = (DateTime)dr1[2];
               dateTimePicker2.Value = (DateTime)dr1[3];
               textBox2.Text = dr1[4].ToString();
               textBox3.Text = dr1[5].ToString();
               textBox4.Text = dr1[].ToString();
           }
           else
           {
               MessageBox.Show("Record can not found");
           }
           dr1.Close();
           conn1.Close();
       }
Posted
Updated 28-Aug-13 2:20am
v2
Comments
[no name] 28-Aug-13 8:21am    
In which textbox you are fetching the value..??
[no name] 28-Aug-13 8:26am    
Are we supposed to guess at what you think is "wrong"? Your SQL query is wrong to begin with. Why are you converting a string to an integer to convert it back to a string? And you should be using parameterized queries to help prevent SQL injection attacks.
Jean A Brandelero 28-Aug-13 8:34am    
If he parse int to string it will prevent injection, fun. haha

1 solution

This line:
textBox4.Text = dr1[].ToString();

Don't have a column index.


Edit:
Why do you convert to Int32 and use the value as a string?
commandstring = "SELECT * FROM productT  WHERE spiceID='" + Convert.ToInt32(textBox1.Text) + "'";

Try this:
commandstring = string.Format("SELECT * FROM productT  WHERE spiceID='{0}'", textBox1.Text.Trim());


Just did a "heal" on your ccde, see this:

private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == string.Empty) return; 
           
            int id = 0;
            if (int.TryParse(textBox1.Text.Trim(), out id))
            {
                String commandstring;
                SqlDataReader dr1;
                SqlCommand com2;

                commandstring = string.Format("SELECT * FROM productT  WHERE spiceID='{0}'", id);

                com2 = new SqlCommand(commandstring, conn1);

                try
                {
                    conn1.Open();
                }
                catch
                {
                    MessageBox.Show("error in seaching", "message", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }

                try
                {
                    dr1 = com2.ExecuteReader();
                    if (dr1.Read())
                    {
                        comboBox1.Text = dr1[1].ToString();
                        dateTimePicker1.Value = (DateTime)dr1[2];
                        dateTimePicker2.Value = (DateTime)dr1[3];
                        textBox2.Text = dr1[4].ToString();
                        textBox3.Text = dr1[5].ToString();
                        textBox4.Text = dr1[].ToString();
                    }
                    else
                    {
                        MessageBox.Show("Record can not found");
                    }
                }
                catch
                {
                    if (!dr1.IsClosed) dr1.Close();
                    conn1.Close();
                } 
            }
        }
 
Share this answer
 
v4
Comments
sachinthasri 28-Aug-13 23:44pm    
I'm a newer to c#.please send your answer with more details.I want to get a values like "1b","No20/B,england(this is a address)"from sql table.'{0}' why you use this?????
sachinthasri 31-Aug-13 22:52pm    
It works.thank you sooo much.

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