Hello, I have this id column and a text column in one table. I want to be able to display the text value, when the id is search inside the database. I have already created the search part, but am unsure on how to get and display the text value. This is my
code:
con = new SqlConnection(connString);
con.Open();
cmd = new SqlCommand("select * from table where ID='" + Label1.Text + "'", con);
da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
//how to be able to get/call the text value and display it on the page?
}
colol
Member
117 Points
542 Posts
How to display value inside sql, based on another value
Nov 08, 2012 05:35 AM|LINK
Hello, I have this id column and a text column in one table. I want to be able to display the text value, when the id is search inside the database. I have already created the search part, but am unsure on how to get and display the text value. This is my code:
con = new SqlConnection(connString); con.Open(); cmd = new SqlCommand("select * from table where ID='" + Label1.Text + "'", con); da = new SqlDataAdapter(cmd); da.Fill(dt); if (dt.Rows.Count > 0) { //how to be able to get/call the text value and display it on the page? }tgyoga
Contributor
2378 Points
393 Posts
Re: How to display value inside sql, based on another value
Nov 08, 2012 08:16 AM|LINK
First of all, you say you are gonna search for a id, so..
You should not be using a label control, change that to a textbox, so that you can key in a ID
Do not use "Select *", always specify the columns that you want to select like.. SELECT Name, ID FROM YourTable WHERE ID = ....
Try to have a stored proc whereever possible, so that your input value can be parameterized.
Before checking for the count of rows, make sure your dt is not NULL...
if (dt != null && dt.Rows.Count > 0) {...}Since you want to display result, first you should be having a Gridview..
if (dt != null && dt.Rows.Count > 0) { gridView1.DataSource = dt; gridView1.DataBind(); }Yoga
colol
Member
117 Points
542 Posts
Re: How to display value inside sql, based on another value
Nov 08, 2012 11:17 PM|LINK
good morning, thank you but I dont understand the part where you say about stored proc, can you give me an example?
Amy Peng - M...
Star
11753 Points
1097 Posts
Microsoft
Re: How to display value inside sql, based on another value
Nov 09, 2012 01:46 AM|LINK
Hi colo1,
Please try to refer to the code about store proc:
Hope it can help you!
Regards,
Amy Peng
Feedback to us
Develop and promote your apps in Windows Store