Hi i need some help with updating changes to multiple rows in the GridView. I was able to add multiple rows simultaneosuly when i click the 'save' button however when i'm trying to update multiple rows i'm getting a problem. Is there is a specific way to that? thanks
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;
using System.Configuration;
namespace WindowsFormsApplication1
{
public partial class Reservations_Form : Form
{
public Reservations_Form()
{
InitializeComponent();
}
DataSet ds = new DataSet();
DataSet changes;
SqlConnection cs = new SqlConnection(@"Data Source=SAIPAYILAVARAPU\newproject2;Initial Catalog=Reservation;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("SELECT *From Customers");
BindingSource tblCustomers = new BindingSource();
DataView dview = new DataView();
// CurrencyManager cmgr;
// DataRowView drv;
private void Form1_Load(object sender, EventArgs e)
{
da.SelectCommand = new SqlCommand("SELECT *FROM Customers", cs);
da.Fill(ds);
dg.DataSource = ds.Tables[0];
tblCustomers.DataSource = ds.Tables[0];
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
//
SqlCommandBuilder cmd = new SqlCommandBuilder(da);
//DataSet changes = new DataSet();
changes = ds.GetChanges();
//cs.Close(); //
//dg.DataSource = ds.Tables[0]; //
if (changes != null)
{
DialogResult button = MessageBox.Show ("Are you sure you want to save this data?","Save Data?", MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
if (button == DialogResult.Yes)
{
da.Update(changes);
}
MessageBox.Show("Changes Done");
}
cs.Close(); //
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public SqlConnection GetSqlConnection() //connection function
{
string str_sqlcon = "Data Source=SAIPAYILAVARAPU\\newproject2;Initial Catalog=Reservation;Integrated Security=True";
SqlConnection mycon = new SqlConnection(str_sqlcon);
mycon.Open();
return mycon;
}
public void getcom(string sqlstr) //function for adding rows
{
SqlConnection sqlcon = this.GetSqlConnection(); // Watch out same string type as GetSQLConnection function
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcom.ExecuteNonQuery();
sqlcom.Dispose();
sqlcon.Close();
sqlcon.Dispose();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult button = MessageBox.Show("Do you want to Cancel the Entering the Data?", "Cancel?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (button == DialogResult.Yes)
{
tbxFirstName.Focus();
}
}
private void btnDisplay_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
da.SelectCommand = new SqlCommand("SELECT *FROM Customers", cs);
da.Fill(ds);
dg.DataSource = ds.Tables[0];
tbxFirstName.DataBindings.Add( new Binding ("Text", tblCustomers, "First_Name"));
tbxFirstName.DataBindings.Clear();
tbxLastName.DataBindings.Add( new Binding ("Text", tblCustomers, "Last_Name"));
tbxLastName.DataBindings.Clear();
//tblReservations.DataSource = ds.Tables[0];
//tbxFirstName.DataBindings.Add(new Binding("Text", tblReservations, "First_Name"));
//tbxLastName.DataBindings.Add(new Binding("Text", tblReservations, "Last_Name"));
}
private void btnSearch_Click_1(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("SELECT *FROM Customers WHERE First_Name = '" + tbxFirstName.Text + "'", cs);
da.Fill(dt);
dg.DataSource = dt;
}
private void btnDelete_Click(object sender, EventArgs e)
{
string col1 = dg[0, dg.CurrentCell.RowIndex].Value.ToString();
string col2 = dg[1, dg.CurrentCell.RowIndex].Value.ToString();
string col3 = dg[2, dg.CurrentCell.RowIndex].Value.ToString();
string col4 = dg[3, dg.CurrentCell.RowIndex].Value.ToString();
string col5 = dg[4, dg.CurrentCell.RowIndex].Value.ToString();
string col6 = dg[5, dg.CurrentCell.RowIndex].Value.ToString();
string col7 = dg[6, dg.CurrentCell.RowIndex].Value.ToString();
string col8 = dg[7, dg.CurrentCell.RowIndex].Value.ToString();
string col9 = dg[8, dg.CurrentCell.RowIndex].Value.ToString();
string sql_update = "DELETE FROM Customers WHERE First_Name = '" + col1 + "'";
DialogResult button = MessageBox.Show("Are you sure you want to Delete the Record?", "Update Record?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (button == DialogResult.Yes)
{
this.getcom(sql_update);
MessageBox.Show("Record Deleted");
}
}
private void tbxFirstName_TextChanged(object sender, EventArgs e)
{
if (tbxFirstName.Text != null)
{
btnSearch.Enabled = true;
}
else
{
btnSearch.Enabled = false;
}
}
private void dg_CellClick(object sender, DataGridViewCellEventArgs e)
{
// btnInsert.Enabled = true;
btnDelete.Enabled = true;
//btnUpdate.Enabled = true;
btnSave.Enabled = true;
btnCancel.Enabled = true;
}
}
}