Click here to Skip to main content
Sign Up to vote bad
good
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:19
stellus762
Edited 27 Feb '13 - 4:29


1 solution

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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 333
1 OriginalGriff 261
2 Mohammed Hameed 198
3 Mayur_Panchal 153
4 Dave Kreskowiak 125
0 Sergey Alexandrovich Kryukov 8,171
1 OriginalGriff 6,236
2 CPallini 3,482
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid