Click here to Skip to main content
13,090,672 members (43,682 online)
Rate this:
Please Sign up or sign in to vote.
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

  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());    
    catch (FormatException ex) { MessageBox.Show(ex.Message); }
catch (SqlException ea) { MessageBox.Show(ea.Message); }
Posted 11-Feb-13 21:03pm
Updated 11-Feb-13 21:50pm
source.compiler 12-Feb-13 3:13am
i dnt understand it ! you want after any insert , delete, update operation, your gridview show the changes?
ontheline89 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 12-Feb-13 3:25am
so you want another datagridview show those records which updated or inserted or deleted before?
ontheline89 12-Feb-13 3:27am
yes i need datagrid to show me specific results for SensorValue < 20
source.compiler 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 12-Feb-13 3:51am
[Edit] added pre tags
Rate this: bad
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[^]
Karthik Harve 12-Feb-13 3:52am
[Edit] added pre tags.
srikanth pachava 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
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

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 |
Web04 | 2.8.170813.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2017
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