Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am adding hexvalue in Table_1 . But if the hexvalue is not AA (or any specified value) then, i need to insert that hexvalue to Table_2. How can i do it ? Thanks
C#
try
{
   SqlConnection sqlconn = new SqlConnection("Data Source=AAN-PC;Initial Catalog=mon26;Integrated Security=True");
   SqlDataAdapter sa = new SqlDataAdapter();

   string query = "insert into Table_1(SensorValue,Time) values(@SV, @TM)";
   sa.InsertCommand = new SqlCommand(query, sqlconn);
   sa.InsertCommand.Parameters.AddWithValue("@SV", hexvalue);
   sa.InsertCommand.Parameters.AddWithValue("@TM", DateTime.Now.ToString());

   sqlconn.Open();
   try
   {
       sa.InsertCommand.ExecuteNonQuery();
    }
    catch (FormatException ex) { MessageBox.Show(ex.Message); }
                MessageBox.Show("Record Inserted");
               
    //  Update Datagridview
    mon26DataSet1.GetChanges();
    table_1TableAdapter.Fill(mon26DataSet1.Table_1);

    sqlconn.Close();
}


[Edit]Pre tag added by Jibesh[/Edit]
Posted
Updated 25-Jan-13 19:57pm
v2
Comments
Jibesh 26-Jan-13 1:54am    
was it difficult to compare the hexvalue in the above code and write a separate query string based on the condition?

1 solution

C#
try
{
   SqlConnection sqlconn = new SqlConnection("Data Source=AAN-PC;Initial Catalog=mon26;Integrated Security=True");
   SqlDataAdapter sa = new SqlDataAdapter();

   string query;
   if(hexvalue =""AA")
    query = "insert into Table_1(SensorValue,Time) values(@SV, @TM)";
   else
    query = "insert into Table_2(SensorValue,Time) values(@SV, @TM)";

   sa.InsertCommand = new SqlCommand(query, sqlconn);
   sa.InsertCommand.Parameters.AddWithValue("@SV", hexvalue);
   sa.InsertCommand.Parameters.AddWithValue("@TM", DateTime.Now.ToString());

   sqlconn.Open();
   try
   {
       sa.InsertCommand.ExecuteNonQuery();
    }
    catch (FormatException ex) { MessageBox.Show(ex.Message); }
                MessageBox.Show("Record Inserted");
               
    //  Update Datagridview
    mon26DataSet1.GetChanges();
    table_1TableAdapter.Fill(mon26DataSet1.Table_1);

    sqlconn.Close();
}
 
Share this answer
 
Comments
ontheline89 26-Jan-13 2:08am    
Thank you so much Jibesh.
Jibesh 26-Jan-13 3:11am    
You welcome
ontheline89 26-Jan-13 3:13am    
Jibesh is it the right way to update the data grid view ?
Jibesh 26-Jan-13 3:35am    
what are you doing with datagridview here? i dont see any code that updates the mon26DataSet1. at which location your dataset gets filled?

to update the datagridview its very simple.
fill the dataset and use datagridview.dataSource = dataSet.Tables[0];

you can find many articles and questions here in CP how use dataset and datagridview
ontheline89 26-Jan-13 3:40am    
oh ok thank you !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900