Click here to Skip to main content
11,813,557 members (46,893 online)
Rate this: bad
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

  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
Edited 11-Feb-13 21:50pm
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
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 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
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
0 Abhinav S 275
1 Palash Mondal_ 190
2 KrunalRohit 150
4 Manas_Kumar 115
0 OriginalGriff 3,877
1 KrunalRohit 2,526
2 Maciej Los 2,245
3 CPallini 2,240
4 ppolymorphe 1,750

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2015
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