I have GridView in windows Form using C# ... The data is showing Successfully a and update can also being done successfully ... but problem is with insertion .... when I try to insert multiple data into database using Iteration then the it stores 13 times into database because by the datagrid initially show 13 records from database ... I meant that it stores new rows multiple times(datagridview rows count) into database ... let suppose I want to save two rows into databse but it stores 1st row into database 13 times(total number of datagridview rows) Please check where I am doing mistake Note: I want to use single datagridview for displaying , update , insert and delete data from database ... I have button through which I want to start insertion all new rows to database here my codes is below string connection = System.Configuration.ConfigurationManager.ConnectionStrings["AuzineConnection"].ConnectionString;
using (SqlConnection sqlconn = new SqlConnection(connection))
{
sqlconn.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.IsNewRow)
{
using (SqlCommand sqlcomm = sqlconn.CreateCommand())
{
//sqlcomm.CommandText = "Insert into QF (profileID, UserNameLogInName, UserFullName,Email, forumtitle, subtitle, subjecttitle ,noreply, noview , qtags ,Question ,Questiondetails ,questionstatus, qdate, Status ,todate) values(@status)";
sqlcomm.CommandText = "Insert into QF (UserNameLogInName,Status ) values(@UserNameLogInName,@status)";
try
{
sqlcomm.Parameters.AddWithValue("UserNameLogInName", dataGridView1.CurrentRow.Cells["UserNameLogInName"].Value.ToString());
sqlcomm.Parameters.AddWithValue("Status", dataGridView1.CurrentRow.Cells["Status"].Value.ToString());
sqlcomm.ExecuteNonQuery();
//MessageBox.Show(dataGridView1.Rows[Convert.ToInt32(RecordIndexNumber.Text)].Cells[Convert.ToInt32(ColInexNum.Text)].Value.ToString());
}
catch (Exception ex)
{
btnInsert.Text = ex.Message.ToString();
}
}
}
}
}
} |
I have tried it if (!row.IsNewRow) also but with it ! this it stores only one row and without it stores multiple unwanted times into database
i also used //for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) //{} but the same issue etier it stores one or multiple times
I just wanted to insert the new rows generated by the user on button click.....
Well the codes which display data are below
private void GetAllData()
{
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["AuzineConnection"].ConnectionString;
using (SqlConnection sqlconn = new SqlConnection(connection))
{
sqlconn.Open();
MessageBox.Show("Connected");
using (SqlCommand sqlcomm = sqlconn.CreateCommand())
{
sqlcomm.CommandText = "Select * From QF";
// SqlDataReader rdr = null;
sda = new SqlDataAdapter(sqlcomm);
dt = new DataTable();
ds = new DataSet();
sda.Fill(dt);
sda.Fill(ds);
// BindingSource bindingSource = new BindingSource();
// bindingSource.DataSource = dt;
try
{
// sqlconn.Open();
if (dt.Rows.Count > 0)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dt;
label1.Text = dataGridView1.RowCount.ToString();
}
}
catch (Exception ex)
{
label1.Text = ex.Message.ToString();
}
}
}
}Hi This is Syed Azy