Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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
Updated 11-Feb-13 21:50pm
v3
Comments
ali_heidari_ 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.
ali_heidari_ 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
ali_heidari_ 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!

you can query the data set
C#
DataTable tblMEN = dsView.Tables["MEN"];
DataRow[] results = tblMen.Select("SensorValue < 20");

reference link
how to run query on dataset[^]
 
Share this answer
 
v2
Comments
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.
You can use Select statement like this.
C#
DataTable dtSensor = feb12DataSet.Tables[0].Select("SensorValue < 20") // use Table index or Name of table
 
Share this answer
 
v2

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