Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have designed a form which fulfills production data manipulation.

C#
AdaptMst.SelectCommand = new SqlCommand("SELECT * FROM Iss_Mst WHERE FromDeptID=8 AND ToDeptID!=8 ORDER BY DATE DESC", baseConnection);
            ADaptDetails.SelectCommand = new SqlCommand("SELECT ID,IssMstID,ToOprID,ToDrgId,BatchNo,Qty,FromOprID,Rew,Back FROM Iss_Dtl",baseConnection);
            try
            {
                baseConnection.Open();
                AdaptMst.Fill(tblMst);
                ADaptDetails.Fill(tblDetails);
                dsInspProd.Tables.Add(tblMst);
                dsInspProd.Tables.Add(tblDetails);

                dsInspProd.Tables[0].TableName = "tblMst";
                dsInspProd.Tables[1].TableName = "tblDetails";

                dsInspProd.Tables[0].Columns["ID"].AutoIncrement = true;
                dsInspProd.Tables[0].Columns["ID"].Unique = true;
                
                dsInspProd.Tables[1].Columns["ID"].AutoIncrement = true;
                dsInspProd.Tables[1].Columns["ID"].Unique = true;
                

                dsInspProd.EnforceConstraints = false;
                DataRelation dr = new DataRelation("IssDtl",dsInspProd.Tables[0].Columns["ID"],dsInspProd.Tables[1].Columns["IssMstID"]);
                dr.Nested = true;
                dsInspProd.Relations.Add(dr);

                bsMaster.DataSource = dsInspProd;
                bsMaster.DataMember = "tblMst";

                bsDetails.DataSource = bsMaster;
                bsDetails.DataMember = "IssDtl";
                bnBase.BindingSource = bsMaster;
                InitCommonData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                baseConnection.Close();
            }

 protected override void tsbtnBaseSave_Click(object sender, EventArgs e)
        {
            base.tsbtnBaseSave_Click(sender, e);
            try
            {

                dsInspProd.AcceptChanges();
                SqlCommandBuilder Updt = new SqlCommandBuilder(AdaptMst);
                AdaptMst.UpdateCommand = Updt.GetUpdateCommand();
                AdaptMst.Update(dsInspProd, "tblMst");

                SqlCommandBuilder UpdtDetails = new SqlCommandBuilder(ADaptDetails);
                ADaptDetails.UpdateCommand = UpdtDetails.GetUpdateCommand();

                SqlCommandBuilder InsertDetails = new SqlCommandBuilder(ADaptDetails);
                ADaptDetails.InsertCommand = InsertDetails.GetInsertCommand();

                ADaptDetails.Update(dsInspProd, "tblDetails");
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
                tsBaseLabel.Text = ex.Message.ToString();
            }
        }


I am able to Update/Edit records, But When I am trying to add a new record, It gives an Instance error.

What is the missign for adding new records in master-detail record...?

I have designed a base form which contains BindingNavigator and BindingSource Objects, the methods of these objects are implemented on the derived form.

Please help.

Thanks,
Bhavin.
Posted

1 solution

Hi,
I just skimmed your code and I just found one fault in the first line:
C#
AdaptMst.SelectCommand = new SqlCommand("SELECT * FROM Iss_Mst WHERE FromDeptID=8 AND ToDeptID!=8 ORDER BY DATE DESC", baseConnection);

You can not use '!=' in SQL scripts so replace it with '<>' .
For more information take a look at these posts:
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/4d10fe87-2de8-42a8-8ef9-b9d46c0fd28d[^]
http://www.dotnettreats.com/tipstricks/howtodal1.aspx[^]
http://forums.asp.net/t/1392048.aspx/1[^]

I hope it helps,
Cheers
 
Share this answer
 
Comments
BhavinBhatt 17-Apr-12 1:48am    
I have made changes in SQL Statement. But it didn't give a solution... !!
walterhevedeich 17-Apr-12 2:37am    
Click on Improve Question and then paste the exact error message, and on what line did your code error out. That way, you can get more help.
BhavinBhatt 17-Apr-12 3:08am    
It didn't give any error but my record is not saved into the DATABASE...

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