Hi. Many programmers have problems creating Invoicing and Billing Software. This article will help them to save the data (Items via DataGridView) in the database. And this is for those basic programmers who don't know how to save data using DataGridView in Window Forms using C# and SQL Server.
Code For Saving Data In Database (Microsoft SQL Server) via DataGridView
In this article I have created a function for collecting the information entered by the use and after that he can save all that information in the database.
void Save()
{
try
{
if (dataGridView1.Rows.Count > 1)
{
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
string col4 = dataGridView1.Rows[i].Cells[3].Value.ToString();
string col5 = dataGridView1.Rows[i].Cells[4].Value.ToString();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
string insert = "INSERT INTO InvoiceItems(SNo,Quantity,Rate,Description,Total) VALUES (@SNo,@Quantity,@Rate,@Description,@Total)";
con.Open();
SqlCommand cmd = new SqlCommand(insert, con);
cmd.Parameters.AddWithValue("@SNo", col1.ToString());
cmd.Parameters.AddWithValue("Description", col2.ToString());
cmd.Parameters.AddWithValue("@Quantity", col3.ToString());
cmd.Parameters.AddWithValue("@Rate", col4.ToString());
cmd.Parameters.AddWithValue("Total", col5.ToString());
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
catch (Exception ex)

waldo sanPosted Mar 6, 2020, 8:24 AM
To me, very good example, just i was looking, thaks Anurag
bobe nutpaPosted Jan 24, 2017, 4:57 AM
(int i = 0; i < dataGridView1.Rows.Count - 1; i++)
bobe nutpaPosted Jan 24, 2017, 4:56 AM
You'll need to change code little bit
Collins AigbekhaiPosted Dec 22, 2014, 5:56 AM
I really apreciate your work it has reduced my curiousity, like Oliver Twist I want more...if I have controls like textbox and datetime picker control on the same form with a datagridview how can I save the data of all controls into database. Second question. If I have a column for discount on the datagridview hw will the calculation look like. I will be grateful if I receive prompt response. Thank you in advance.
Bhanu Pratap SinghPosted Dec 16, 2014, 11:35 PM
use of stored procedure may mitigate the security issues of plain query used for insert statement. i will suggest developers to use bool functions. anyway nice for beginners.
Dnyaneshwar BabarPosted Jan 24, 2014, 5:26 AM
please provide update and delete operation in windows applications
Anurag SarkarPosted Jun 16, 2013, 1:05 AM
What do you want ?
steve scottPosted Jun 15, 2013, 4:26 PM
Okay then I suck because I am having a hard time reproducing it LOL. maybe you could assist me with?
steve scottPosted Jun 15, 2013, 3:54 AM
Wow, I can't tell you how long I have been looking for an example like this. I have spoke to many programmers who were unable to assist me in writing code like this. I am developing an estimating package in C# and C where I will be implementing code like this (as soon as I can understand how it works). It seems very simple however I still can't grasp it yet-I am a beginner. So, my questions are as follows: 1) Can you make a sampel like this in C without the save to SQL procedure? 2) I cant seem to make adjustments to your sample-why is this? Is it write protected, or am I inexperienced? 3) Would you be interested in developing a sample in C (C# will work too) with about 11 formulas and about 20 cells, with my formulas? I would pay you for your time. If you would like to speak further my addie is steve-scott hotmal , pleas contact me. Thank you!
Sam HobbsPosted Mar 18, 2013, 12:52 PM
This could be helpful. I think however that beginners should first learn the Entity Framework (EF) or maybe Table Adapters. The EF is newer. They are called "typed databases" and will do most of what this code does. t helps to understand the details shown in this article but I think it is best to start with typed databases.