0

i have consistently asked for help but none so far. Kindly assist me to get the data grid view items saved into a database.below is code for what av done so far

Regards.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace datagrid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void SaveDataGridItems()
        {
            string column1 = string.Empty;
            string column2 = string.Empty;
            string column3 = string.Empty;

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                column1 = Convert.ToString(row.Cells["Column1"].Value);
                column2 = Convert.ToString(row.Cells["Column2"].Value);
                column3 = Convert.ToString(row.Cells["Column3"].Value);

                saveToDB(column1, column2, column3);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            saveToDB(string column1, string column2, string column3);
        }
        public void saveToDB(string column1, string column2, string column3) 
        {
            //do your saving here
            //connection string
            string connstring = "Data Source=IT\\SQLEXPRESS;Initial Catalog=fairdealdb;Integrated Security=SSPI;Persist Security Info=False;";

            //create sql connection
            SqlConnection sqlconn = new SqlConnection(connstring);

            try
            {
                //open the connection
                if (sqlconn.State != ConnectionState.Open)
                {
                    sqlconn.Open();
                }
                SqlCommand sqlcomm = sqlconn.CreateCommand();
                //StringBuilder builder = new StringBuilder();
                //builder.Append("'"+column1.ToString+"'");
                //.Append("'"+column2.ToString+"'");
                //builder.Append("'"+column3.ToString+"'");

                sqlcomm.CommandText = "INSERT INTO datagrid(column1,column2,column3)VALUES()";
            }
            catch{}
        }

        
    }
    }

thats where am stuck at.

2
Contributors
1
Reply
27
Views
6 Years
Discussion Span
Last Post by finito
0
sqlcomm.CommandText = "INSERT INTO YourTable (YourColumn1, YourColumn1, YourColumn1) VALUES ('" + column1 +"', '" + column2 + "', '" + column3 + "')";
sqlcomm.ExecuteNonQuery();

for YourTable you want to put your Table i.e. Employee, Customer, etc.
For YourColumn1 ~ YourColumn3 you want to use your actual columns i.e. FirstName, MiddleName, LastName, EmployeeID, etc.

Sample

new MySqlCommand("INSERT INTO ProdEditRecord (ProductID, DateEdited, Source, QtyAdded, DCID, DCPrice, Remark, SalesPerson, Location, culprit) VALUES (" + ((ProductID.Equals("-1")) ? "" + getMaxProdID() : ProductID) + ", '" + System.DateTime.Now.ToString("yyyyMMdd") + "', '" + ((Source.Equals("")) ? "Non-DC" : Source) + "', " + ((textBox5.Text.Equals("")) ? "0" : textBox5.Text) + ", '" + ((DCID.Equals("")) ? "Non-DC" : DCID) + "', " + ((DCPrice.Equals("")) ? "0.000" : DCPrice) + ", '" + ((Remark.Equals("")) ? " " : Remark + " Invoice Date: " + dateTimePicker1.Value.ToString("yyyyMMdd")) + "', " + Employee + ", '" + CurrectLoc + "', '" + System.Environment.UserName + "')", myConn).ExecuteNonQuery();

This is a sample from my code.

Hope this clarifies your problem.

Please mark as solved if solved.

Edited by finito: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.