Insert Records in Database from datagridview. my code is--
SqlDataAdapter da = new SqlDataAdapter("select * from GradeDGV", con);
da.InsertCommand = new SqlCommand("insert into GradeDGV (Proportions,Percentage,Quantity) values(@Proportions, @Percentage, @Quantity)", con);
da.InsertCommand.Parameters.Add("@Proportions", SqlDbType.VarChar, 10, "Proportions");
da.InsertCommand.Parameters.Add("@Percentage", SqlDbType.Decimal, 10, "Percentage");
da.InsertCommand.Parameters.Add("@Quantity", SqlDbType.Decimal, 10, "Quantity");
DataSet ds = new DataSet();
da.Fill(ds);
DGV_grade.DataSource = ds;
tell me solution as soon as possible.
Loading
Pravin GhadgePosted Dec 17, 2011, 5:09 AM
da = new SqlDataAdapter();
DataRow vDr;
da.SelectCommand = new SqlCommand();
da.SelectCommand.CommandType = CommandType.Text;
vCmBld = new SqlCommandBuilder(da);
da.SelectCommand.Connection = con;
da.SelectCommand.CommandText = "Select * from GradeDGV";
da.Fill(vDs, "GradeDGV");
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
vDr = vDs.Tables["GradeDGV_PAYMENT"].NewRow();
vDr["SrNo"] = dataGridView1.Rows[i].Cells["SrNo"].Value;
//specifed ur column name
vDs.Tables["GradeDGV"].Rows.Add(vDr);
}
}
da.Update(vDs, "GradeDGV");
vDs.Dispose();
da.Dispose();
Pravin GhadgePosted Dec 17, 2011, 7:35 AM
try this
SqlDataAdapter da = new SqlDataAdapter();
DataRow vDr;
da.SelectCommand = new SqlCommand();
da.SelectCommand.CommandType = CommandType.Text;
SqlCommandBuilder vCmBld = new SqlCommandBuilder(da);
da.SelectCommand.Connection = con;
da.SelectCommand.CommandText = "Select * from GradeDGV";
da.Fill(vDs, "GradeDGV");
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
vDr = vDs.Tables["GradeDGV_PAYMENT"].NewRow();
vDr["SrNo"] = dataGridView1.Rows[i].Cells["SrNo"].Value;
//specifed ur column name
vDs.Tables["GradeDGV"].Rows.Add(vDr);
}
}
da.Update(vDs, "GradeDGV");
vDs.Dispose();
da.Dispose();
ashwini bhingolePosted Dec 17, 2011, 6:12 AM
but in my code some error occured.
error--object reference not set to an instance of an object.
my code is--
da = new SqlDataAdapter();
DataRow vDr;
da.SelectCommand = new SqlCommand();
da.SelectCommand.CommandType = CommandType.Text;
vCmBld = new SqlCommandBuilder(da);
da.SelectCommand.Connection = con;
da.SelectCommand.CommandText = "Select * from GradeDGV";
da.Fill(vDs, "GradeDGV");
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
vDr = vDs.Tables["GradeDGV_PAYMENT"].NewRow();
vDr["SrNo"] = dataGridView1.Rows[i].Cells["SrNo"].Value;
//specifed ur column name
vDs.Tables["GradeDGV"].Rows.Add(vDr);
}
}
da.Update(vDs, "GradeDGV");
vDs.Dispose();
da.Dispose();
Manoj Singh PanwarPosted Dec 17, 2011, 5:53 AM
http://www.aspdotnetfaq.com/Faq/How-to-insert-row-in-GridView-with-SqlDataSource.aspx
Accept the answer as correct if it helps you.