Click here to Skip to main content
15,911,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Design as follows in Datagridview ;

Days session 1 session 2

1 check box check box
2 check box check box

i want to save the above record in the database .

for that how can i do using csharp?

to insert the above checkbox values into the msaccess database.

Please help me.

Regards,
Narasiman P.
Posted

to save the value in database like query like this
SQL
SQL="insert into table1(checkboxcolumn1,checkboxcolumn2)values(" + (Convert.ToString(dgv[column,row])=="True" ? -1:0) + "," + (Convert.ToString(dgv[column,row])=="True" ? -1:0) + ")"
 
Share this answer
 
Try this
C#
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=application.accdb;Persist Security Info=True";
       OleDbConnection conn = new OleDbConnection();
       conn.ConnectionString = connStr;

       OleDbDataAdapter adapter = new OleDbDataAdapter();
       adapter.InsertCommand = new OleDbCommand();
       foreach (DataGridViewRow dgvr in datagrid1.Rows)
       {
           // Get the underlying datarow
           DataRow dr = ((DataRowView)dgvr.DataBoundItem).Row;

           // I am assuming your tabel contains three column Id int, Session1 boolean,  Session2 boolean          

           adapter.InsertCommand.CommandText = "INSERT INTO table (Id, Session1, Session2)" + " VALUES(" + dr["Days"] + ", " + (dr["session 1"])?1:0 + "', '" + (dr["session 2"])?1:0 + "');";
           conn.Open();
           adapter.InsertCommand.Connection = conn;
           adapter.InsertCommand.ExecuteNonQuery();
       }
 
Share this answer
 
Try this

C#
SQL="insert into table1(checkboxcolumn1,checkboxcolumn2)values('" +checkBox1.IsChecked.ToString()+"','"+checkBox2.IsChecked.ToString()+"')";
 
Share this answer
 
Please learn the correct way to do this and remember one cardinal rule for working when creating dynamic queries like this. That rule is to always use paramaterized queries. Never use concatenated strings as is shown in Solutions 1, 2 and 3.

C#
var checkbox1Ctl = yourGrid.FindControl("checkbox1ControlName") AS CheckBox;
var checkbox2Ctl = yourGrid.FindControl("checkbox1ControlName") AS CheckBox;
var checkbox3Ctl = yourGrid.FindControl("checkbox1ControlName") AS CheckBox;

using (SqlConnection connection = new SqlConnection(connectionString))
{
   commandText = "INSERT INTO [yourTableName] ( col1, col2, col3 ) VALUES ( @col1Value, @col2Value, @col3Value )";
   SqlCommand command = new SqlCommand(commandText, connection);

   command.Parameters.AddWithValue("@col1Value", checkbox1Ctl != null ? checkbox1Ctl.Checked : false);
   command.Parameters.AddWithValue("@col2Value", checkbox2Ctl != null ? checkbox1Ct2.Checked : false)
   command.Parameters.AddWithValue("@col3Value", checkbox3Ctl != null ? checkbox1Ct3.Checked : false);;

   try
   {
      connection.Open();
      Int32 rowsAffected = command.ExecuteNonQuery();
      Console.WriteLine("RowsAffected: {0}", rowsAffected);
   }
   catch (Exception ex)
   {
      Console.WriteLine(ex.Message);
   }
}


For more information on this topic: SQLCommand.Parameters[^]
 
Share this answer
 

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