SQL Server > SQL Server Forums > SQL Server Data Access > updating data through Datagridview control

Unanswered updating data through Datagridview control

  • Monday, April 30, 2012 5:38 AM
     
     

    In my winform application I want to update data directly through datagridview control

    but that is not happening and there is no error also.Specially when only a single record is

    populated in datagridview control.No updates are happening.How to do it?

    Thanks in advance.

All Replies

  • Monday, April 30, 2012 8:17 AM
    Moderator
     
     

    Hello,

    Please, could you provide the code you are using to update your database ? Could you explain how the code updating the database is executed ( thru a click on a button ? ) ?

    Are you using the DataSoource property of your DataGridView to bind your table and your DataGridView ?

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx

    You may also use a BindingSource also.

    Please, could you tell us the version and edition of your DBMS ? Is it a SQL Server ?

    Please, could you provide also your connection string ? ( i know that it is possible to have problems with a SQL Server Express Edition )

    We are waiting for your feedback to try to help you more efficiently.

    Have a nice day


    Mark Post as helpful if it provides any help.Otherwise,leave it as it is.

  • Thursday, May 03, 2012 9:18 AM
     
     

    I am fetching data with the help of stored proc as per the parameter in my datagridview control.then I update data  in my datagridview control with the help of button event.

    but it does not get modify and throws tablemapping error.

    Code to fetch data on button click event :

    ConnectionStringSettings mycon = ConfigurationManager.ConnectionStrings["DataConnectionString1"];
                        conn = new SqlConnection(mycon.ConnectionString);
                        cmd = new SqlCommand();
                        cmd.Connection = conn;
                        cmd.Connection.Open();
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "dbo.[StdChequefeesPerExam]";

                        SqlParameter parastd = new SqlParameter();

                        parastd.ParameterName = "@Stdname";
                        parastd.Value = combxstdnames.SelectedValue.ToString();
                        cmd.Parameters.Add(parastd);

                        SqlParameter paraattempt = new SqlParameter();
                        paraattempt.ParameterName = "@attemptno";
                        paraattempt.Value = txtAttemptNum.Text;
                        cmd.Parameters.Add(paraattempt);

                        SqlParameter paraexam = new SqlParameter();
                        paraexam.ParameterName = "@examid";
                        paraexam.Value = comboxExamId.SelectedValue.ToString();
                        cmd.Parameters.Add(paraexam);



                        adap = new SqlDataAdapter();
                        adap.SelectCommand = cmd;
                        cmdbuilder = new SqlCommandBuilder(adap);
                        ds = new DataSet();
                        adap.Fill(ds, "ChequeRecptTbl");
                        RecptMasterGridvw.DataSource = ds;
                        RecptMasterGridvw.DataMember = "ChequeRecptTbl";
                        cmd.Connection.Close();

    Another button that modifies data of a datagridview.

    try
                {
                    cmdbuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                   //error Update unable to find TableMapping['ChequeRecptTbl'] or //DataTable'ChequeRecptTbl'.
                    adap.Update(ds, "ChequeRecptTbl");
                   
                    RecptMasterGridvw.Refresh();
                    UpdstatusLbl.Text = "Record Updated";
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }

    what is the problem?

    Thanks in advance.