Im trying to insert values in ms acess db using datagridview checkbox .
I want to insert attendance recordsto the db so i have written the code such that the user can mark the attendance of current date ,and if he trys to insert attendance for the same ppl on the same date then i want to show error
ill show my code but the error is that im not able to insert the values , im getting an error saying connection open,when infact i want the connection open only
code:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[1].Value != null)
{
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
string query = "select WorkerName from AttendanceTable where AttendanceDate= Date()";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string workername = reader["WorkerName"].ToString();
reader.Close();
if (DateTime.Now.ToString() != workername)
{
if ((Boolean)row.Cells[1].Value == true)
{
try
{
string a = Convert.ToString(row.Cells[0].Value.ToString());
int b = Convert.ToInt32(row.Cells[1].Value = -1);
command.CommandText = "insert into AttendanceTable(WorkerName,AttendanceDate,AtWork) values('" + a + "','" + dateTimePicker1.Text + "','" + b + "')";
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
if ((Boolean)row.Cells[1].Value == false)
{
try
{
string a = Convert.ToString(row.Cells[0].Value.ToString());
int b = Convert.ToInt32(row.Cells[1].Value = 0);
command.CommandText = "insert into AttendanceTable(WorkerName,AttendanceDate,AtWork) values('" + a + "','" + dateTimePicker1.Text + "','" + b + "')";
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
connect.Close();
}
}
}
}
MessageBox.Show("Data Saved");
Loading
Bhushan GawalePosted Jan 22, 2015, 1:09 PM
Something like
var result = select * from table where primarykey='a';
if(result.Count==0)
{
//your logic to insert a record in the database
}
Sheffer FernandesPosted Jan 22, 2015, 11:25 AM
but my issue is that if the user marks the same attendance twice on the same day then duplicate values get stored fr eg. a,b,c,ds attendance are marked and saved for today,then if the user trys to mark attendance again for today then i dont want to allow him to mark the attendance
this is the code:
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
connect.Open();
command.CommandText = "select WorkerName from AttendanceTable where AttendanceDate=Date()";
OleDbDataReader reader = command.ExecuteReader();
// MessageBox.Show(reader["WorkerName"].ToString());
int count=0;
while (reader.Read())
{
count++;
}
connect.Close();
if (row.Cells[1].Value != null)
{
if ((Boolean)row.Cells[1].Value == true)
{
try
{
connect.Open();
string a = Convert.ToString(row.Cells[0].Value.ToString());
int b = Convert.ToInt32(row.Cells[1].Value = -1);
command.CommandText = "insert into AttendanceTable(WorkerName,AttendanceDate,AtWork) values('" + a + "','" + dateTimePicker1.Text + "','" + b + "')";
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
if ((Boolean)row.Cells[1].Value == false)
{
try
{
connect.Open();
string a = Convert.ToString(row.Cells[0].Value.ToString());
int b = Convert.ToInt32(row.Cells[1].Value = 0);
command.CommandText = "insert into AttendanceTable(WorkerName,AttendanceDate,AtWork) values('" + a + "','" + dateTimePicker1.Text + "','" + b + "')";
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
}
connect.Close();
if (count == 1)
{
MessageBox.Show("Data Saved");
}
else if (count > 1)
{
MessageBox.Show("Duplictate Records");
}
}
}
Bhushan GawalePosted Jan 22, 2015, 9:02 AM
About your new issue, can you explain in details what duplicates you are referring to?
Sheffer FernandesPosted Jan 22, 2015, 8:22 AM
Bhushan GawalePosted Jan 22, 2015, 2:59 AM
http://www.codeproject.com/Articles/8477/Using-ADO-NET-for-beginners
You can also refer the documentation of best practices in ADO.NET e.g. for using connection objects
https://msdn.microsoft.com/en-us/library/ms971481.aspx#adonetbest_topic5
Sheffer FernandesPosted Jan 22, 2015, 2:43 AM
Bhushan GawalePosted Jan 21, 2015, 1:24 PM
something like
using(OldeDbConnection connection = new OldeDbConnection(....))
{
//your code
}