Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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
Updated 27-Feb-13 4:29am
v2

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();
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900