Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server selection
Hello, I am inserting Records in a Database. I need my records to be shown in a Datagridview, when a button is pressed. Datagridview should show records only with SensorValue is < 20.
I know, that SELECT, from where statement will be used but dont know , how should i use it here.
Thank you
 
try
{
 
  SqlConnection sqlconn = new SqlConnection("Data Source=AN-PC;Initial Catalog=Feb12;Integrated Security=True");
 SqlDataAdapter sa = new SqlDataAdapter();
 
 string query;
 
 query = "insert into Table_1( SensorID,SensorValue,DateTime) values( @TM, @NT, @MN)";
 
    sa.InsertCommand = new SqlCommand(query, sqlconn);
    sa.InsertCommand.Parameters.AddWithValue("@TM", y);
    sa.InsertCommand.Parameters.AddWithValue("@NT", x);
    sa.InsertCommand.Parameters.AddWithValue("@MN", DateTime.Now.ToString());    
                                                                       
    sqlconn.Open();
    try
    {
        sa.InsertCommand.ExecuteNonQuery();
    }
    catch (FormatException ex) { MessageBox.Show(ex.Message); }
 
    feb12DataSet.GetChanges();
 
    table_1TableAdapter4.Fill(feb12DataSet.Table_1);
 
    
    sqlconn.Close();
}
catch (SqlException ea) { MessageBox.Show(ea.Message); }
Posted 11-Feb-13 22:03pm
Edited 11-Feb-13 22:50pm
v3
Comments
source.compiler at 12-Feb-13 3:13am
   
i dnt understand it ! you want after any insert , delete, update operation, your gridview show the changes?
ontheline89 at 12-Feb-13 3:16am
   
Datagridview is showing the records in a Database, but i need other datagridview to show the records when the button is pressed for Select Statement.
source.compiler at 12-Feb-13 3:25am
   
so you want another datagridview show those records which updated or inserted or deleted before?
ontheline89 at 12-Feb-13 3:27am
   
yes i need datagrid to show me specific results for SensorValue < 20
source.compiler at 12-Feb-13 3:40am
   
you can before delete any record in the table, save that record into a datatable !
before inserting any new record, save it on same datatable !
before updating , after operation, save that record into same datatable !
so you have a datatable include operated records! show it with new gridview!
Karthik Harve at 12-Feb-13 3:51am
   
[Edit] added pre tags
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you can query the data set
DataTable tblMEN = dsView.Tables["MEN"];
DataRow[] results = tblMen.Select("SensorValue < 20");
reference link
how to run query on dataset[^]
  Permalink  
v2
Comments
Karthik Harve at 12-Feb-13 3:52am
   
[Edit] added pre tags.
srikanth pachava at 12-Feb-13 4:09am
   
when you made the the changes to database that are reflecting the dataset. so you can queary the dataset is the best choice.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use Select statement like this.
 DataTable dtSensor = feb12DataSet.Tables[0].Select("SensorValue < 20") // use Table index or Name of table 
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2014
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