HELP!!!! DataReader error "No data exists for the row/column"
-
My code below gives the error "No data exists for the row/column." Sub FindZNumber() 'Declare variable to hold ZNumber entered Dim strZNumber As String = txtZNumber.Text.ToString() 'Start to build select (*) statement to see if a valid ZNumber Dim strSQLZN As String strSQLZN = "SELECT Count(*) As TestCount FROM Table1 WHERE ZNUMBER = '" & strZNumber & "'" Dim dsn As String = ConfigurationSettings.AppSettings("dsn") ODBCConn.Open() Dim cmdZN As New Odbc.OdbcCommand(strSQLZN, ODBCConn) Dim myRDR As Odbc.OdbcDataReader = cmdZN.ExecuteReader() lblTest.Text = myRDR("TestCount") ODBCConn.Close() End Sub I'm trying to populate the label (lblTest) with the count from the SQL String
-
My code below gives the error "No data exists for the row/column." Sub FindZNumber() 'Declare variable to hold ZNumber entered Dim strZNumber As String = txtZNumber.Text.ToString() 'Start to build select (*) statement to see if a valid ZNumber Dim strSQLZN As String strSQLZN = "SELECT Count(*) As TestCount FROM Table1 WHERE ZNUMBER = '" & strZNumber & "'" Dim dsn As String = ConfigurationSettings.AppSettings("dsn") ODBCConn.Open() Dim cmdZN As New Odbc.OdbcCommand(strSQLZN, ODBCConn) Dim myRDR As Odbc.OdbcDataReader = cmdZN.ExecuteReader() lblTest.Text = myRDR("TestCount") ODBCConn.Close() End Sub I'm trying to populate the label (lblTest) with the count from the SQL String
you need to call Read method of reader before accessing any row. The read method points the reader to first row. So, in your code, add myRDR.Read() before lblTest.Text = myRDR("TestCount") Sanjay Sansanwal www.sansanwal.com
-
My code below gives the error "No data exists for the row/column." Sub FindZNumber() 'Declare variable to hold ZNumber entered Dim strZNumber As String = txtZNumber.Text.ToString() 'Start to build select (*) statement to see if a valid ZNumber Dim strSQLZN As String strSQLZN = "SELECT Count(*) As TestCount FROM Table1 WHERE ZNUMBER = '" & strZNumber & "'" Dim dsn As String = ConfigurationSettings.AppSettings("dsn") ODBCConn.Open() Dim cmdZN As New Odbc.OdbcCommand(strSQLZN, ODBCConn) Dim myRDR As Odbc.OdbcDataReader = cmdZN.ExecuteReader() lblTest.Text = myRDR("TestCount") ODBCConn.Close() End Sub I'm trying to populate the label (lblTest) with the count from the SQL String
-
If you want to return an aggregate value such as COUNT() use the ExecuteScalar method of the OdbcCommand object. Remember to cast the return value as required. eg. lblTest.Text = CStr(cmdZN.ExecuteScalar())
-
you need to call Read method of reader before accessing any row. The read method points the reader to first row. So, in your code, add myRDR.Read() before lblTest.Text = myRDR("TestCount") Sanjay Sansanwal www.sansanwal.com