hi friends
i want to save the datagrid view checkbox checked row in database.
if iam checked one row if it is having in database it should be updated and if it should not having it shoud be saved in database.
like this can any one create stored procedure.
and code in asp.net.
please help me.
Loading
Sudhakar ChaudharyPosted Jan 26, 2013, 1:25 PM
if u have a auto increment id filed in your database than load your data in girdview and use
loop check the id filed cell in girdview if it is null than save it otherwise update.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace checkBox
{
public partial class Form1 : Form
{
SqlConnection con = ConMgr.GetConnection();
public Form1()
{
InitializeComponent();
}
private void GRIDCOL()
{
try
{
DataGridViewCheckBoxColumn objCheck = new DataGridViewCheckBoxColumn();
objCheck.HeaderText = "SELECT";
objCheck.Name = "chk";
objCheck.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
objCheck.FlatStyle = FlatStyle.Standard;
objCheck.ThreeState = false;
objCheck.CellTemplate = new DataGridViewCheckBoxCell();
dataGridView1.Columns.Insert(0, objCheck);
dataGridView1.Columns.Add("", "SL.");
dataGridView1.Columns.Add("", "NAME");
dataGridView1.Columns.Add("", "FATHER");
dataGridView1.Columns.Add("", "DOB");
dataGridView1.Columns.Add("", "MOBILE");
dataGridView1.Columns.Add("", "ADDRESS");
dataGridView1.Columns.Add("", "QUALIFICATION");
dataGridView1.Columns[1].ReadOnly = true;
dataGridView1.Columns[0].Width = 20;
dataGridView1.Columns[1].Width = 30;
dataGridView1.Columns[2].Width = 120;
dataGridView1.Columns[3].Width = 120;
dataGridView1.Columns[4].Width = 80;
dataGridView1.Columns[5].Width = 80;
dataGridView1.Columns[6].Width = 90;
dataGridView1.Columns[7].Width = 130;
dataGridView1.RowHeadersVisible = false;
dataGridView1.BackgroundColor = Color.White;
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Red;
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
}
catch (Exception) { }
}
private void GridFill()
{
try
{
dataGridView1.Rows.Clear();
string sSql = "SELECT SNO, NAME, FATHERNAME,CONVERT(VARCHAR(10), DATEOFBIRTH,103), MOBNUMBER, ADDRESS, QUALIFICATION FROM TB";
System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(sSql, con);
DataSet ds = new DataSet();
adp.Fill(ds, "s");
if (ds.Tables["s"].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables["s"].Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[1].Value = ds.Tables["s"].Rows[i].ItemArray[0];
dataGridView1.Rows[i].Cells[2].Value = ds.Tables["s"].Rows[i].ItemArray[1];
dataGridView1.Rows[i].Cells[3].Value = ds.Tables["s"].Rows[i].ItemArray[2];
dataGridView1.Rows[i].Cells[4].Value = ds.Tables["s"].Rows[i].ItemArray[3];
dataGridView1.Rows[i].Cells[5].Value = ds.Tables["s"].Rows[i].ItemArray[4];
dataGridView1.Rows[i].Cells[6].Value = ds.Tables["s"].Rows[i].ItemArray[5];
dataGridView1.Rows[i].Cells[7].Value = ds.Tables["s"].Rows[i].ItemArray[6];
}
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void Form1_Load(object sender, EventArgs e)
{
GRIDCOL();
}
private void button1_Click(object sender, EventArgs e)
{
GridFill();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
SqlDataAdapter adp;
SqlCommand cmd = new SqlCommand();
string Query;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
{
//check if id exits
Query = "SELECT * FROM TB WHERE SNO='" + dataGridView1.Rows[i].Cells[1].Value + "'";
adp = new SqlDataAdapter(Query, con);
adp.Fill(ds, "s");
if (ds.Tables["s"].Rows.Count > 0)
{
//update
Query = "UPDATE tb SET NAME ='" + dataGridView1.Rows[i].Cells[2].Value + "', FATHERNAME ='" + dataGridView1.Rows[i].Cells[3].Value + "', DATEOFBIRTH ='" + dataGridView1.Rows[i].Cells[4].Value + "', MOBNUMBER ='" + dataGridView1.Rows[i].Cells[5].Value + "', ADDRESS ='" + dataGridView1.Rows[i].Cells[6].Value + "', QUALIFICATION ='" + dataGridView1.Rows[i].Cells[7].Value + "' WHERE SNO='" + dataGridView1.Rows[i].Cells[1].Value + "'";
cmd.CommandText = Query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
}
else
{
//save
Query = "INSERT INTO tb(NAME, FATHERNAME, DATEOFBIRTH, MOBNUMBER, ADDRESS, QUALIFICATION) VALUES('" + dataGridView1.Rows[i].Cells[2].Value
+ "','" + dataGridView1.Rows[i].Cells[3].Value
+ "','" + dataGridView1.Rows[i].Cells[4].Value
+ "','" + dataGridView1.Rows[i].Cells[5].Value
+ "','" + dataGridView1.Rows[i].Cells[6].Value
+ "','" + dataGridView1.Rows[i].Cells[7].Value + "')";
cmd.CommandText = Query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
}
ds.Tables["s"].Rows.Clear();
}
}
MessageBox.Show("Record Save Successfully.");
}
catch (Exception EX) { MessageBox.Show(EX.ToString()); }
}
}
}