Anjello Joshua Anjello Joshua - 1 month ago 21
C# Question

Linking Textboxes C# SQL Server

How do I link textboxes?

Scenario:

TextBox_Supplier
TextBox_Address


TextBox_Supplier
is autocomplete and it's working. When typing is done in
TextBox_Supplier
, the
TextBox_Address
will select supplier's address.

My code does not work:

private void txb_vendor_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txb_address.Text))
{
PurCon.con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = PurCon.getcon();
cmd.CommandType = CommandType.Text;
cmd.CommandText = String.Format("SELECT address FROM tbl_Supplier WHERE supplier_name = {0}",txb_vendor.Text);

SqlDataReader red = cmd.ExecuteReader();

while (red.Read())
{
string address = red.GetString(0);
address = txb_address.Text;
}

PurCon.con.Close();
}
}


Thank you for helping me!

Answer

Do Change as @Mohit suggested and also wrap the supplier name with single quotes, since supplier name is string type and in sql String should be wrap in single Quotes other wise this will give SQL Error

"SELECT address FROM tbl_Supplier WHERE supplier_name = '{0}'"
                                                    ----^