BrasilianGuy
IS-IT--Management
wrote this script below to retrieve data from a specific table.
private void savenewid()
{
Connection.Open();
String Newproductid = productid.Text;
String SQLStatementInsert = "insert into releasedids (newid) Values ("+ Newproductid+")";
SqlCommand Commandinsert = new SqlCommand(SQLStatementInsert, Connection);
SqlDataReader insertReader = Commandinsert.ExecuteReader();
lblresult.Text = "The New Id you entered has been Saved";
btnSave.Visible = false;
insertReader.Close();
Connection.Close();
Connection.Open();
String SQLStatementselectnewid = "SELECT * FROM productrefresh WHERE productnumber = " + Newproductid;
SqlCommand dataCommand = new SqlCommand(SQLStatementselectnewid, Connection);
SqlDataReader dataReader = dataCommand.ExecuteReader();
if (dataReader.HasRows)
{
String Strproduct = dataReader.GetString(0);
String SQLStatementDelete = "UPDATE prodreminder set product =" + Newproductid + " 2 WHERE product =" + Strproduct;
SqlCommand CommandDelete = new SqlCommand(SQLStatementDelete, Connection);
CommandDelete.ExecuteNonQuery();
lblresult2.Text = "This product has reminders request";
}
else
{
lblresult2.Text = "This product does not have any reminder request";
}
dataReader.Close();
Connection.Close();
}
It gives me this error “Invalid attempt to read when no data is present.” In the highlighted line right after the if statement.
I know the query is returning a row which explains why it is going into the if statement but I can’t figure out why it is not grabing the result from that specific cell (GetString(0)).
Thnaks
private void savenewid()
{
Connection.Open();
String Newproductid = productid.Text;
String SQLStatementInsert = "insert into releasedids (newid) Values ("+ Newproductid+")";
SqlCommand Commandinsert = new SqlCommand(SQLStatementInsert, Connection);
SqlDataReader insertReader = Commandinsert.ExecuteReader();
lblresult.Text = "The New Id you entered has been Saved";
btnSave.Visible = false;
insertReader.Close();
Connection.Close();
Connection.Open();
String SQLStatementselectnewid = "SELECT * FROM productrefresh WHERE productnumber = " + Newproductid;
SqlCommand dataCommand = new SqlCommand(SQLStatementselectnewid, Connection);
SqlDataReader dataReader = dataCommand.ExecuteReader();
if (dataReader.HasRows)
{
String Strproduct = dataReader.GetString(0);
String SQLStatementDelete = "UPDATE prodreminder set product =" + Newproductid + " 2 WHERE product =" + Strproduct;
SqlCommand CommandDelete = new SqlCommand(SQLStatementDelete, Connection);
CommandDelete.ExecuteNonQuery();
lblresult2.Text = "This product has reminders request";
}
else
{
lblresult2.Text = "This product does not have any reminder request";
}
dataReader.Close();
Connection.Close();
}
It gives me this error “Invalid attempt to read when no data is present.” In the highlighted line right after the if statement.
I know the query is returning a row which explains why it is going into the if statement but I can’t figure out why it is not grabing the result from that specific cell (GetString(0)).
Thnaks