Hi,
I have designed the database in which there are two columns named Class and roll no. Now I want to retrieve all the roll no with a specific class. i will enter the class in the textbox and will press the button.I expect that all the Records which satisfy my query must be displayed in data grid view.
i have written the following code for the above but it has some error.
private void button1_Click(object sender, EventArgs e)
{
// database name is "nish"
SqlConnection con = new SqlConnection("data source=(local)\\SQLEXPRESS;initial catalog=nish;integrated security=true");
SqlDataAdapter sda;
DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
string b = textBox1.Text;
//MessageBox.Show(b);
string qry = "select * from tab where class="+b;
//MessageBox.Show(qry);
sda = new SqlDataAdapter(qry, con);
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
|
|
if anybody can find out the problem in my prog. or has some different to my solution please reply as soon as possible.
thanks in advance.
Satyapriya NayakPosted Jun 13, 2010, 12:25 AM
Here is the code:-
Note:- See the bold part of the code.
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from student where Class='" +TextBox1.Text+ "'";
cmd = new SqlCommand(str,con);
ad = new SqlDataAdapter(cmd);
ds = new DataSet();
ad.Fill(ds,"student");
dataGridView1.DataMember = "student";
dataGridView1.DataSource = ds;
con.Close();
Thanks
If it helps you plz mark it as Accepted Answer
}