คำตอบ updating data through Datagridview control

  • 30 เมษายน 2555 5:38
     
     

    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.

ตอบทั้งหมด

  • 30 เมษายน 2555 8:17
    ผู้ดูแล
     
     

    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.

  • 3 พฤษภาคม 2555 9:18
     
     

    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.


  • 9 พฤษภาคม 2555 16:04
    ผู้ดูแล
     
     

    Hello,

    1st problem : you have forgotten to specify the size of the parameters

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx

    The definition of the table would be useful ( the full CREATE TABLE ).Idem the dbo.[StdChequefeesPerExam] stored procedure

    There is something i have not understood  "Another button that modifies data of a datagridview". Are you trying to update the contain of the datagridview or of the table(s) of your database ? I am sorry , it is not clear.

    If you have modified the contain of your datagridview and you want to update , delete or insert in the database, you have to use an AcceptChanges() method to validate your modifications.

    http://msdn.microsoft.com/en-us/library/system.data.dataset.acceptchanges.aspx

    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.


    • แก้ไขโดย Papy NormandModerator 9 พฤษภาคม 2555 16:07 Added the link towards AcceptChanges and corrected AcceptChange to AcceptChanges
    •  
  • 28 สิงหาคม 2555 7:06
     
     
    Hello,
    I am not able to update columns that are of smallmoney datatype through datagridview.what is the issue not getting.
  • 28 สิงหาคม 2555 11:43
    ผู้ดูแล
     
     

    Hello,

    I am glad to meet you nearly 3 months after the last post of this thread.

    Please, could you have a look at this link ?

    http://msdn.microsoft.com/en-us/library/cc716729(VS.90).aspx

    You have to "translate" the value of your "smallmoney" column of your datagridview from a string to a the Decimal .Net type.

    As we don't know what the full version/edition of your SQL Server ( and even whether you are using SQL Server ) , it is really difficult to help you more efficiently.

    It could be useful to know the structure of the table(s) and if you are using stored procedures to perform the SELECT/INSERT/UPDATE/DELETE queries

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

    Have a nice day

    PS : Have you solved the question you are asked in your 1st post ? ( if yes, the solution would be appreciated as it could be useful for other potential posters having the same problems )


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

  • 8 กันยายน 2555 20:21
    ผู้ดูแล
     
     

    Hello srm 2010 ,

    Please, could you tell us whether you have succeeded to solve your problem.

    If yes, please , could you provide your solution as it would be useful for the people who could face the same problem ?

    Have a nice day


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

  • 11 กันยายน 2555 6:29
     
     คำตอบ

    Hello,

    I have got the solution to my problem with smallmoney datatype.It works fine after I made changes in stored proc.

    Thanks

    • ทำเครื่องหมายเป็นคำตอบโดย Papy NormandModerator 11 กันยายน 2555 9:29
    •