Click here to Skip to main content
12,627,340 members (35,539 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# SQL-Server
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
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 25-Jan-13 20:17pm
Updated 25-Jan-13 20:57pm
Jibesh17.5K
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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();
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 26 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100