Code
Line 25: da = new SqlDataAdapter(strQuery, con);Code
Line 26: DataSet ds = new DataSet();Code
Line 27: da.Fill(ds,"Students");Code
Line 28:Code
Line 29:ViewState["Sql_Query"] = strQuery;i didnt understand why i am getting this error and how do i solve it?
my Codes:
string strQuery = "Select * from tblStudent where ID = " + txtID.Text;
da = new SqlDataAdapter(strQuery, con);
DataSet ds = new DataSet();
da.Fill(ds,"Students");

Khargesh RajputRank #616 · 3.9K reputationPosted Apr 29, 2015, 8:12 PM EDT
debug your code here
Select * from tblStudent where ID = " + Convert.ToInt32(txtID.Text);// if id int
or
Select * from tblStudent where ID = '" + txtID.Text+"'";
best way is
debug code
and check strquery
copy and paste in sql and execute if your query is wrong then edit it
Syed ShanuRank #87 · 23.7K reputationPosted Apr 29, 2015, 4:23 PM EDT
da.Fill(ds);
or
da.Fill(ds,"tblStudent");
Posted Apr 30, 2015, 10:43 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Apr 30, 2015, 11:25 AM EDT
Second - Since you are only dealing with a single table from your database then you should use DataTable and not DataSet.
Here's a quick example:
protected void BindGrid(){
DataTable dt = new DataTable();
using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){
string sql = "Select * from tblStudent where ID = @ID";
using(SqlCommand cmd = new SqlCommand(sql,connection)){
connection.Open();
cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(txtID.Text));
using(SqlDataAdapter ad = new SqlDataAdapter(cmd)){;
ad.Fill(dt);
//check if the query returns any data
if (dt.Rows.Count > 0) {
GridView1.DataSource = dt;
GridView1.DataBind();
}
else{
//No records found
}
}
}
}
}
protected void Page_Load(object sender, EventArgs e){
if(!IsPostback)
BindGrid();
}
Posted Apr 30, 2015, 12:04 PM EDT