Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a form to where you can enter a name in a textbox and it populates the next textbox. FOr some reason it is not working. What am I doing wrong? Here is my code:

C#
 protected void TextBoxLongName_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con2.Open();

            SqlCommand scmd2 = new SqlCommand("Select INST_ID, LongName from Table23 where LongName = '" + TextBoxINST_ID.Text + "'", con2);
            SqlDataReader dr2 = scmd2.ExecuteReader();

            if (dr2.Read())

            {
                TextBoxINST_ID.Text = dr2["INST_ID"].ToString();
            }
            dr2.Close();
            con2.Close();
        }
    }
}
Posted
Comments
CHill60 12-May-14 11:39am    
Define "not working"
Computer Wiz99 12-May-14 11:48am    
It is not populating the textbox. When you enter a name in TextboxLongName and hit tab key or click anywhere on the form it should populate the TextBoxINST_ID but it does not. What did I do wrong?
Kornfeld Eliyahu Peter 12-May-14 11:50am    
You may not hit the line at all! Debug it!
Computer Wiz99 12-May-14 11:52am    
What do you mean?
Kornfeld Eliyahu Peter 12-May-14 11:53am    
This line is conditional: TextBoxINST_ID.Text = dr2["INST_ID"].ToString();
Maybe the condition is false!!!

1 solution

Your code almost right.
You have given exact match condition. May be user input was wrong

See one more wrong input is here you are using TextBoxLongName textbox for input and search and this TextBoxINST_ID for to populate result. Hope so this is wrong so you have change this this.
protected void TextBoxLongName_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con2.Open();
 
            SqlCommand scmd2 = new SqlCommand("Select INST_ID, LongName from Table23 where LongName = '" + TextBoxLongName.Text + "'", con2);
            SqlDataReader dr2 = scmd2.ExecuteReader();
 
            if (dr2.Read())
 
            {
                TextBoxINST_ID.Text = dr2["INST_ID"].ToString();
            }
            dr2.Close();
            con2.Close();
        }
    }
}
 
Share this answer
 
v2
Comments
Computer Wiz99 12-May-14 12:47pm    
I copied the name from the database and pasted it in the textbox and nothing populated. What else could it be?
[no name] 12-May-14 13:12pm    
see above context is updated

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