how to find the return value of reader in mysql
-
hi, am selecting data from table . and executed reader with rdr=cmd.executereader() based on the result i need to write the logic. if( returns null) { } else { }
-
hi, am selecting data from table . and executed reader with rdr=cmd.executereader() based on the result i need to write the logic. if( returns null) { } else { }
simple
if(rdr.HasRows == true)
{
//do stuff
}
else
{
//do no rows returned stuff
}here is an example for you. http://stackoverflow.com/questions/12609959/how-to-check-if-sqldatareader-has-no-rows[^]
Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON
-
simple
if(rdr.HasRows == true)
{
//do stuff
}
else
{
//do no rows returned stuff
}here is an example for you. http://stackoverflow.com/questions/12609959/how-to-check-if-sqldatareader-has-no-rows[^]
Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON
eventhough it's not working properly. here is the code
string query1 = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM transaction where tag_id Like '" + sEPC + "' AND out_time=NULL";
MySqlCommand cmd1 = new MySqlCommand(query1, connection);
MySqlDataReader rdr1 = cmd1.ExecuteReader();
//int c = rdr1.RecordsAffected;
// MessageBox.Show("count" + c);
//if (rdr1 !=null && rdr1.Read() && rdr1.HasRows)
if (rdr1.HasRows==true)
{
//ListViewItem item1 = new ListViewItem();
item.SubItems[OUTTime.Index].Text = s2;
//item.SubItems[5].Text = s2;
//ListView1_EPC.Items.Add(item);
//ListView1_EPC.Items[0].SubItems[OUTTime.Index].Text = s2;
rdr1.Close();
String s1 = "update transaction set out_time='" + s2 + "' where tag_id='" + sEPC + "' ";
MySqlCommand cmd2 = new MySqlCommand(s1, connection);
cmd2.ExecuteNonQuery();} else { rdr1.Close(); item.SubItems\[INTime.Index\].Text = s2; // ListView1\_EPC.Items.Add(item); String a0 = ListView1\_EPC.Items\[0\].Text; String a1 = item.SubItems\[1\].Text; String a2 = item.SubItems\[2\].Text; String a3 = item.SubItems\[3\].Text; String a4 = item.SubItems\[4\].Text; String a5 = item.SubItems\[5\].Text; String str = "insert into transaction(tag\_id,category\_id,product\_id,product\_name,gate\_id,in\_time,remarks)values('" + a0+ "','" + a1 + "','" + a2 + "','" + a3 + "','" + a4 + "','" +s2 + "' ,'"+a5+"')";
-
eventhough it's not working properly. here is the code
string query1 = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM transaction where tag_id Like '" + sEPC + "' AND out_time=NULL";
MySqlCommand cmd1 = new MySqlCommand(query1, connection);
MySqlDataReader rdr1 = cmd1.ExecuteReader();
//int c = rdr1.RecordsAffected;
// MessageBox.Show("count" + c);
//if (rdr1 !=null && rdr1.Read() && rdr1.HasRows)
if (rdr1.HasRows==true)
{
//ListViewItem item1 = new ListViewItem();
item.SubItems[OUTTime.Index].Text = s2;
//item.SubItems[5].Text = s2;
//ListView1_EPC.Items.Add(item);
//ListView1_EPC.Items[0].SubItems[OUTTime.Index].Text = s2;
rdr1.Close();
String s1 = "update transaction set out_time='" + s2 + "' where tag_id='" + sEPC + "' ";
MySqlCommand cmd2 = new MySqlCommand(s1, connection);
cmd2.ExecuteNonQuery();} else { rdr1.Close(); item.SubItems\[INTime.Index\].Text = s2; // ListView1\_EPC.Items.Add(item); String a0 = ListView1\_EPC.Items\[0\].Text; String a1 = item.SubItems\[1\].Text; String a2 = item.SubItems\[2\].Text; String a3 = item.SubItems\[3\].Text; String a4 = item.SubItems\[4\].Text; String a5 = item.SubItems\[5\].Text; String str = "insert into transaction(tag\_id,category\_id,product\_id,product\_name,gate\_id,in\_time,remarks)values('" + a0+ "','" + a1 + "','" + a2 + "','" + a3 + "','" + a4 + "','" +s2 + "' ,'"+a5+"')";
Define not working properly? also to read from the reader you have to do this
if(rdr1.HasRows == true)
{
while (rdr1.Read())
{
//have made an assumption here that you have a ID field in the data
//so that you can see how to get data back from the reader.
int id = Convert.ToInt(rdr1["ID"]);
}
}Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON
-
Define not working properly? also to read from the reader you have to do this
if(rdr1.HasRows == true)
{
while (rdr1.Read())
{
//have made an assumption here that you have a ID field in the data
//so that you can see how to get data back from the reader.
int id = Convert.ToInt(rdr1["ID"]);
}
}Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON
no i don't want to read the data of reader. datatype of out_time and in_time in mysql table is datetime. my problem is: 1.select the data from transaction where tagid=? and outtime=null 2. if reader is having rows then write outtime into transaction table. else insert row into transaction table with only intime, no outtime here is the code :
string query1 = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM transaction where tag_id Like '" + sEPC + "' AND out_time=NULL";
MySqlCommand cmd1 = new MySqlCommand(query1, connection);
MySqlDataReader rdr1 = cmd1.ExecuteReader();if (rdr1.HasRows==true) { item.SubItems\[OUTTime.Index\].Text = s2; rdr1.Close(); String s1 = "update transaction set out\_time='" + s2 + "' where tag\_id='" + sEPC + "' "; MySqlCommand cmd2 = new MySqlCommand(s1, connection); cmd2.ExecuteNonQuery(); } else { rdr1.Close(); item.SubItems\[INTime.Index\].Text = s2; String a0 = ListView1\_EPC.Items\[0\].Text; String a1 = item.SubItems\[1\].Text; String a2 = item.SubItems\[2\].Text; String a3 = item.SubItems\[3\].Text; String a4 = item.SubItems\[4\].Text; String a5 = item.SubItems\[5\].Text; String str = "insert into transaction(tag\_id,category\_id,product\_id,product\_name,gate\_id,in\_time,remarks)values('" + a0+ "','" + a1 + "','" + a2 + "','" + a3 + "','" + a4 + "','" +s2 + "' ,'"+a5+"')"; MySqlCommand cmd2 = new MySqlCommand(str, connection); cmd2.ExecuteNonQuery();
-
Define not working properly? also to read from the reader you have to do this
if(rdr1.HasRows == true)
{
while (rdr1.Read())
{
//have made an assumption here that you have a ID field in the data
//so that you can see how to get data back from the reader.
int id = Convert.ToInt(rdr1["ID"]);
}
}Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON
ok i got it. thanks for the help.. But now the problem is , i can have multiple records with same tag_id, when i update all the records with same tag_id is updating. i need to update only the latest record. how can i solve this.......