Reputation: 71
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();
Upvotes: 0
Views: 1663
Reputation: 71
I was approaching it in the wrong way. Basically the following was the correct code:
<asp:SqlDataSource ID="SupplierCompanyDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROGConnectionString %>"
SelectCommand="SELECT [Company], [SupplierID] FROM [SupplierDB] WHERE ([SupplierID] >= @SupplierID)">
<SelectParameters>
<asp:Parameter DefaultValue="14" Name="SupplierID" Type="Int32" /></SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SupplierCountryDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROGConnectionString %>"
SelectCommand="SELECT [SupplierID], [Country] FROM [SupplierDB] WHERE ([SupplierID] = @SupplierID)">
<SelectParameters> <asp:ControlParameter ControlID="SupplierListBox" Name="SupplierID" PropertyName="SelectedValue" Type="Int32" /></SelectParameters></asp:SqlDataSource>
This does the job! So now when I click on the listbox1 the listbox2 also gets clicked similar to cascading.
Upvotes: 0
Reputation: 460138
Well, it's not clear what's the main problem, so i'll show you a working example which selects the Country
column from database with ADO.NET, uses Parameters to avoid SQL-Injection and using
-statement to ensure that all unmanaged resources as the connection get disposed(closed).
string sql = @"
SELECT Country
FROM dbo.Supplier
WHERE SupplierID = @SupplierId";
using (var con = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True"))
{
using (var cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.Add("@SupplierId", SqlDbType.Int);
cmd.Parameters["@SupplierId"].Value = int.Parse(SupplierListBox.SelectedItem.Value);
using (var rdr = cmd.ExecuteReader())
{
if (rdr.Read())
{
TextBox1.Text = rdr.GetString(0);
}
}
}
}
Upvotes: 2
Reputation: 62841
You need to look into using parameterized queries. Try this out:
string SupplierListvalue = SupplierListBox.SelectedItem.Value; //SupplierListvalue retrieves the SupplierID value
...
SqlCommand cmd = new SqlCommand("select Country from SupplierDB WHERE SupplierID = @supplierId", conn);
cmd.Parameters.AddWithValue("@supplierId", SupplierListvalue);
...
Good luck.
Upvotes: 0