Search code examples
c#datatablesqlhelper

Confusion of sql helper class


I want to display list of names and roll no of students who are studying in nursery stored in my database......But there seems to be an error in following code which i can't figure out

 protected void NurseryButton_Click(object sender, EventArgs e)
{

    SQLHelper sqhlpr = new SQLHelper();
    sqhlpr.SqlText = "Select StudentName,RollNo from tblStudentInfo where Class=@Class";
    sqhlpr.AddParameter("Class", sender.ToString());
    sqhlpr.ExecuteScalar(false);
    DataTable dt = sqhlpr.getDataTable(false);
    Label1.Text = dt.Rows[0]["StudentName"].ToString();
}

The error goes like this"There is no row at position 0" in my last line of text i.e.in label1.text. Can anybody help me??


Solution

  • You have to use SqlHelper.ExecuteDataSet so get the result set of your query. ExecuteScalar resutns a single value from row. Example

    DataTable dt = SqlHelper.ExecuteDataSet(ConnString,
                                     CommandType.Text,
                                     "Select StudentName,RollNo from tblStudentInfo where Class=@Class",
                                     parameter).Tables[0];