Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is my code but button2_Click(object sender, EventArgs e) is not working, it was supposed to auto update gridview to database. incase its showing error:

Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace Customer
{
    public partial class frmGridVw : Form
    {
        private String connectionString = null;
        private SqlConnection sqlConnection = null;
        private SqlDataAdapter sqlDataAdapter = null;
        private SqlCommandBuilder sqlCommandBuilder = null;
        private DataTable dataTable = null;
        private BindingSource bindingSource = null;
        private String selectQueryString = null;
        
        public frmGridVw()
        {
            InitializeComponent();
        }

        private void frmGridVw_Load(object sender, EventArgs e)
        {
            //   GridVw.Columns.Add("gvName" , "name");
            //showAllData();
            // txtgdvPhoneSearch.Focus();
            // GridVw.Enabled = false;
            Customer.DB.DbCon_call showDb = new Customer.DB.DbCon_call();
            try
            {
                showDb.con.Open();
                //da = new SqlDataAdapter("SELECT CustID as 'Customer ID', CustName as Name,CustPhoneNo as 'Phone No' , CustAddress as Address, CustBloodGroup as 'Blood Group', CustFatherName as 'Father''s Name', CustMotherName as 'Mother''s Name' FROM r_customer", showDb.con);

                selectQueryString = "SELECT CustID as 'Customer ID', CustName as Name,CustPhoneNo as 'Phone No' , CustAddress as Address, CustBloodGroup as 'Blood Group', CustFatherName as 'Father''s Name', CustMotherName as 'Mother''s Name' FROM r_customer";
                sqlDataAdapter = new SqlDataAdapter(selectQueryString, showDb.con);
                sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

                //                ds = new DataSet();
                dataTable = new DataTable();
                sqlDataAdapter.Fill(dataTable);
                bindingSource = new BindingSource();
                bindingSource.DataSource = dataTable;
                GridVw.DataSource = bindingSource;
            }
            catch (Exception ex)
            {
                if (showDb.con.State.Equals("Open"))
                {
                   // showDb.con.Close();
                }
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        public void button1_Click(object sender, EventArgs e)
        {
            GridVw.Enabled = true;
            //  showAllData();
           // searchData();
        }

        //private void GridVw_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    //string str;
        //    //str = GridVw.SelectedCells[0].Value.ToString();
        //    //textBox1.Text= str;
        //}

        public void showAllData()
        {
            Customer.DB.DbCon_call showDb = new Customer.DB.DbCon_call();
            try
            {
                showDb.con.Open();
                //da = new SqlDataAdapter("SELECT CustID as 'Customer ID', CustName as Name,CustPhoneNo as 'Phone No' , CustAddress as Address, CustBloodGroup as 'Blood Group', CustFatherName as 'Father''s Name', CustMotherName as 'Mother''s Name' FROM r_customer", showDb.con);

                selectQueryString = "SELECT CustID as 'Customer ID', CustName as Name,CustPhoneNo as 'Phone No' , CustAddress as Address, CustBloodGroup as 'Blood Group', CustFatherName as 'Father''s Name', CustMotherName as 'Mother''s Name' FROM r_customer";
                sqlDataAdapter = new SqlDataAdapter(selectQueryString, showDb.con);

                sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
                
                //                ds = new DataSet();
                dataTable = new DataTable();
                sqlDataAdapter.Fill(dataTable);
                bindingSource = new BindingSource();
                bindingSource.DataSource = dataTable;
                GridVw.DataSource = bindingSource;
            }
            catch (Exception ex)
            {
                if (showDb.con.State.Equals("Open"))
                {
                    showDb.con.Close();
                }
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        public void searchData()
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Customer.DB.DbCon_call showDb = new Customer.DB.DbCon_call();
            try
            {
                showDb.con.Open();
                sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
                MessageBox.Show(exceptionObj.Message.ToString());
               // txtgdvPhoneSearch.Text = exceptionObj.Message;
            }

        }
    }
}
Posted
Updated 18-Jul-11 8:44am
v3

1 solution

You need to return the table's primary key column.
 
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