Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to save datagridview data to database in c# windows application

private void btn_Save_Click(object sender, EventArgs e)
{
    string constring = @"Data Source=databasename;Initial Catalog=Rookies;Integrated Security=True";
    SqlConnection con = new SqlConnection(constring);
    SqlTransaction transaction = con.BeginTransaction();
    try
    {
        con.Open();
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO tbl_prospects ([prospectid], [firstname], [lastname], [height], [weight], [age], [college])VALUES(@prospectid, @firstname, @lastname, @height, @weight, @age, @college)", con))
            {
                cmd.Parameters.AddWithValue("@prospectid", row.Cells["prospectid"].Value);
                cmd.Parameters.AddWithValue("@firstname", row.Cells["firstname"].Value);
                cmd.Parameters.AddWithValue("@lastname", row.Cells["lastname"].Value);
                cmd.Parameters.AddWithValue("@height", row.Cells["height"].Value);
                cmd.Parameters.AddWithValue("@weight", row.Cells["weight"].Value);
                cmd.Parameters.AddWithValue("@age", row.Cells["age"].Value);
                cmd.Parameters.AddWithValue("@college", row.Cells["college"].Value);
                cmd.Transaction = transaction;
                cmd.ExecuteNonQuery();
            }
        }
        transaction.Commit();
        con.Close();
        MessageBox.Show("Successfully Saved!");
    }
    catch (Exception ex)
    {
        transaction.Rollback();
        con.Close();
        MessageBox.Show(ex.Message);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# read file from directory 
Csharp :: unity reverse string 
Csharp :: why v-slot not working in vue 3 
Csharp :: unity quaternion 
Csharp :: unity c# foreach 
Csharp :: httpclient post c# example 
Csharp :: how to verify the scene unity 
Csharp :: c# winforms textbox select text 
Csharp :: string to biginteger c# 
Csharp :: how to remove space between string in c# 
Csharp :: how to put double quotes in a string c# 
Csharp :: arrays in c# 
Csharp :: add text to combobox c# 
Csharp :: c# byte 
Csharp :: get out of foreach statement c# 
Csharp :: doing void when gameobject setactive unity 
Csharp :: what is type unity 
Csharp :: color unity 
Csharp :: play animation through script unity 
Csharp :: c# convert double to int 
Csharp :: unity ihandler click right button 
Csharp :: event trigger by code unity 
Csharp :: unity find object by name 
Csharp :: how to save a dictionary as a csv file in c# 
Csharp :: convert xml string to file c# 
Csharp :: c# radio button checked 
Csharp :: if statement c# 
Csharp :: c# replace dash in string 
Csharp :: c# random 
Csharp :: c# get property type of list 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =