Line 25: da = new SqlDataAdapter(strQuery, con);
Line 26: DataSet ds = new DataSet();
Line 27: da.Fill(ds,"Students"); Line 28:
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");
Posted Apr 30, 2015, 4:04 PM
Vincent Maverick DuranoPosted Apr 30, 2015, 3:25 PM
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, 2:43 PM
Khargesh RajputPosted Apr 30, 2015, 12:12 AM
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 ShanuPosted Apr 29, 2015, 8:23 PM
da.Fill(ds);
or
da.Fill(ds,"tblStudent");