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.