I am trying to query data of a specific column text from another column.
Basically, I have a Supplier Database that has a SupplierID and Country column.
I already have the SupplierID for that specific row for example, it is 14. I would like to get the text value of Country column based on the 14 value.
The Supplier ID I am getting by the following code (listbox):
<asp:ListBox ID="SupplierListBox" runat="server"
DataSourceID="SupplierCompanyDataSource" DataTextField="Company"
DataValueField="SupplierID" Width="315px"
Height="80px"
onselectedindexchanged="SupplierListBox_SelectedIndexChanged"
AutoPostBack="True"></asp:ListBox>
Code:
string SupplierListvalue = SupplierListBox.SelectedItem.Value; //SupplierListvalue retrieves the SupplierID value
SqlDataReader rdr = null;
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select Country from SupplierDB", conn);
cmd.Connection = conn;
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
TextBox1.Text = rdr["Country"].ToString();
MessageBox.Show("Connection Successful");
MessageBox.Show(rdr.ToString());
}
conn.Close();
Well, it’s not clear what’s the main problem, so i’ll show you a working example which selects the
Countrycolumn from database with ADO.NET, uses Parameters to avoid SQL-Injection andusing-statement to ensure that all unmanaged resources as the connection get disposed(closed).