Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi every one,
 
am developing windows application using c#.net, here i need to update and delete in datagridview, actually i done the above one that is which is updated and deleted in my side.
but the thing is the data cannot update in database but update in datagridview the same way has also deleting option, so please find out my code below where i did mistake why am not updating in database. please give me a solution.
 
thanks in advance...
 
my entire code is:
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;
using System.Configuration;
 

namespace tarriff_design_page
{
    public partial class testing : Form
    {
        
        // string s = @"Data Source=.\SQLEXPRESS;Initial Catalog=tarrif;User ID=sa;Password=sa";

        
 
        private SqlDataAdapter da;
        private SqlConnection conn;
        BindingSource bsource = new BindingSource();
        DataSet ds = null;
        string sql;
 

        public testing()
        {
            InitializeComponent();
          
            LoadData();
            
        }
        public static String hh
        {
            get
            {
                
            }
        }
 
       
 
        private void btnupdate_Click(object sender, EventArgs e)
        {
           
            try
{
DialogResult result = MessageBox.Show("Are you sure you want to proceed Updation ?", "Update Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
SqlConnection CN = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=tarrif;User ID=sa;Password=sa");
CN.Open();
 
string query = "Select * from test";
 
SqlDataAdapter dAdapter = new SqlDataAdapter(query, CN);
 
SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);
 
DataTable dTable = new DataTable();
 
dAdapter.Fill(dTable);
 
DataGridView DGview = new DataGridView();
 
BindingSource bSource = new BindingSource();
 
DGview.DataSource = bSource;
 
dAdapter.Update(dTable);
 
CN.Close();
 
}
else
{
 
}
 
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
 
        }
 
        private void btndelete_Click(object sender, EventArgs e)
        {
            try
{
DialogResult result = MessageBox.Show("Are you sure you want to delete this row?", "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
 
if (result == DialogResult.Yes)
{
foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
{
if (dataGridView1.Rows.Count > 1)
dataGridView1.Rows.Remove(dr);
}
}
                }
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
        }
 

 
        public void LoadData()
        {
            string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=tarrif;User ID=sa;Password=sa";
            conn = new SqlConnection(connectionString);
            sql = "select * from test";
            da = new SqlDataAdapter(sql, conn);
            conn.Open();
            ds = new DataSet();
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
            da.Fill(ds, "test");
            bsource.DataSource = ds.Tables["test"];
            dataGridView1.DataSource=bsource;
        }
 
        private void testing_Load(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
Posted 27-Feb-13 4:19am
Edited 27-Feb-13 4:29am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
If you want to update data into DB, you should use Update statement in btndelete_Click event.
 
SqlConnection CN = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=tarrif;User ID=sa;Password=sa");
CN.Open();

string query = "Update Table 'Table Name' Set 'column_name'='value' where 'Some condition'";
 
SqlCommand Cmd = new SqlCommand(query , CN);
Cmd.Connection = CN;
Cmd.ExecuteNonQuery();
CN.Close();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 Richard Deeming 240
2 _Amy 205
3 CHill60 195
4 Nirav Prabtani 178
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,045
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100