Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 5th, 2005, 02:27 PM
Registered User
 
Join Date: Sep 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Update unable to find TableMapping

I am receiving the following error on my update statement.

System.InvalidOperationException: Update unable to find TableMapping['Table'] or DataTable 'Table'.

I am trying to update the database based on the line the user selects in the datagrid. I perform a second select from the database soley for the line the user selects. I then assign the values i want from that row to each line in the datagrid. The command builder was unable to automatically create the update statement so i created it manually. Please help!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

namespace LFS_Incidents_Update.ClientServices
{
    /// <summary>
    /// Summary description for Update_Multi_Base_Merge.
    /// </summary>
    public class Update_Multi_Base_Merge : System.Web.UI.Page
    {

        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.HyperLink HyperLink1;
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        public SqlConnection sc = new SqlConnection("workstation id=QED_IT4;packet size=4096;user id=sa;data source=devsvr1;persist security info=False;initial catalog=laaf_o");
        public DataSet baseDS = new DataSet();
        public SqlDataAdapter sda = new SqlDataAdapter();
        public void Page_Load(object sender, System.EventArgs e)
        {

            sc.Open();
            SqlCommand sCmd = sc.CreateCommand();
            sda.SelectCommand = sCmd;
            sCmd.CommandType = CommandType.Text;
            //sCmd.CommandText = "SELECT iQuoteId,siLineNumber,chSegmentDesc,iMergeSegmentQ ty FROM mergeSegment WHERE iQuoteId="+ Request.QueryString.Get("iQuoteId");
            //sCmd.CommandText = "Select mergeSegment.iQuoteId,mergeSegment.siLineNumber,me rgeSegment.chSegmentDesc,mergeSegment.iMergeSegmen tQty,x.chProductNumber,x.iProductCatId FROM mergeSegment join (Select iQuoteId,siLineNumber,mergeSegmentDetail.chProduct Number,iProductCatId from mergeSegmentDetail INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber where laaf_l.dbo.ProductInfo.bIsBaseProduct=1) x on mergeSegment.iQuoteId = x.iquoteid and mergeSegment.siLineNumber = x.silinenumber where mergeSegment.iQuoteId="+Request.QueryString.Get("i QuoteId")+" order by mergeSegment.siLineNumber";
                sCmd.CommandText="SELECT mergeSegmentDetail.iQuoteId, mergeSegmentDetail.siLineNumber,mergeSegmentDetail .chProductNumber,mergeSegmentDetail.iProductCatId FROM mergeSegmentDetail INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber WHERE laaf_l.dbo.ProductInfo.bIsBaseProduct=1 and mergeSegmentDetail.iQuoteId=" +Request.QueryString.Get("iQuoteId");
            if(IsPostBack == false)
            {
                sda.Fill(baseDS,"laaf_o.dbo.mergeSegmentDetail");
                DataGrid1.DataBind();



                int loop1;
                // Load NameValueCollection object.
                NameValueCollection coll=Request.QueryString;
                // Get names of all keys into a string array.
                String[] arr1 = coll.AllKeys;
                for (loop1 = 0; loop1 < arr1.Length; loop1++)
                {
                    String[] arr2 = coll.GetValues(arr1[loop1]);
                }
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            foreach(DataGridItem DataGridItem in DataGrid1.Items)
            {
                if(DataGridItem.Cells[0].Controls[1] is CheckBox)
                {
                    CheckBox Checkbox1 = (CheckBox)DataGridItem.Cells[0].Controls[1];
                    if(Checkbox1.Checked == true)
                    {


                        SqlCommand sCmd1 = sc.CreateCommand();
                        SqlDataAdapter sda1 = new SqlDataAdapter();
                        sda1.SelectCommand = sCmd1;
                        sCmd1.CommandType = CommandType.Text;
                        sCmd1.CommandText = "SELECT mergeSegmentDetail.iQuoteId, mergeSegmentDetail.siLineNumber,mergeSegmentDetail .chProductNumber,mergeSegmentDetail.iProductCatId FROM mergeSegmentDetail INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber WHERE laaf_l.dbo.ProductInfo.bIsBaseProduct=1 and mergeSegmentDetail.iQuoteId=" +Request.QueryString.Get("iQuoteId")+ "and mergeSegmentDetail.siLineNumber=" +TextBox1.Text;
                        DataSet baseDS1 = new DataSet();
                        sda1.Fill(baseDS1);

                        DataGridItem.Cells[2].Text = baseDS1.Tables[0].Rows[0].ItemArray[2].ToString();
                        DataGridItem.Cells[3].Text = baseDS1.Tables[0].Rows[0].ItemArray[3].ToString();

                        SqlCommand sCmd2 = sc.CreateCommand();

                        sda.UpdateCommand = sCmd2;
                        sCmd2.CommandType = CommandType.Text;

                        //sCmd2.CommandText = "Update mergeSegmentDetail SET mergeSegmentDetail.iProductCatId ="+baseDS1.Tables[0].Rows[0].ItemArray[2]+",mergeSegmentDetail.chProductNumber ="+baseDS1.Tables[0].Rows[0].ItemArray[3]+ " INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber WHERE laaf_l.dbo.ProductInfo.bIsBaseProduct=1 and iQuoteId ="+Request.QueryString.Get("iQuoteId")+" and siLineNumber="+DataGridItem.Cells[2].Text;
                        sCmd2.CommandText = "Update mergeSegmentDetail SET mergeSegmentDetail.iProductCatId =@iProductCatId,mergeSegmentDetail.chProductNumber =@chproductNumber INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber WHERE laaf_l.dbo.ProductInfo.bIsBaseProduct=1 and iQuoteId ="+Request.QueryString.Get("iQuoteId")+" and siLineNumber="+DataGridItem.Cells[2].Text;
                        sCmd2.Parameters.Add("@iProductCatId",SqlDbType.In t,4,"iProductCatId");
                        sCmd2.Parameters.Add("@chProductNumber",SqlDbType. Int,4,"chProductNumber");
                        sda.Update(baseDS);
                    }
                }

            }
            sda.Fill(baseDS);
            DataGrid1.DataBind();
            sc.Close();
        }
    }
}



 
Old December 5th, 2005, 04:42 PM
Registered User
 
Join Date: Sep 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

updated code but still recieving same error:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

namespace LFS_Incidents_Update.ClientServices
{
    /// <summary>
    /// Summary description for Update_Multi_Base_Merge.
    /// </summary>
    public class Update_Multi_Base_Merge : System.Web.UI.Page
    {

        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.HyperLink HyperLink1;
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        public SqlConnection sc = new SqlConnection("workstation id=QED_IT4;packet size=4096;user id=sa;data source=devsvr1;persist security info=False;initial catalog=laaf_o");
        public DataSet baseDS = new DataSet();
        public SqlDataAdapter sda = new SqlDataAdapter();
        public void Page_Load(object sender, System.EventArgs e)
        {

            sc.Open();
            SqlCommand sCmd = sc.CreateCommand();
            sda.SelectCommand = sCmd;
            sCmd.CommandType = CommandType.Text;
            //sCmd.CommandText = "SELECT iQuoteId,siLineNumber,chSegmentDesc,iMergeSegmentQ ty FROM mergeSegment WHERE iQuoteId="+ Request.QueryString.Get("iQuoteId");
            //sCmd.CommandText = "Select mergeSegment.iQuoteId,mergeSegment.siLineNumber,me rgeSegment.chSegmentDesc,mergeSegment.iMergeSegmen tQty,x.chProductNumber,x.iProductCatId FROM mergeSegment join (Select iQuoteId,siLineNumber,mergeSegmentDetail.chProduct Number,iProductCatId from mergeSegmentDetail INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber where laaf_l.dbo.ProductInfo.bIsBaseProduct=1) x on mergeSegment.iQuoteId = x.iquoteid and mergeSegment.siLineNumber = x.silinenumber where mergeSegment.iQuoteId="+Request.QueryString.Get("i QuoteId")+" order by mergeSegment.siLineNumber";
                sCmd.CommandText="SELECT mergeSegmentDetail.iQuoteId, mergeSegmentDetail.siLineNumber,mergeSegmentDetail .chProductNumber,mergeSegmentDetail.iProductCatId FROM mergeSegmentDetail INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber WHERE laaf_l.dbo.ProductInfo.bIsBaseProduct=1 and mergeSegmentDetail.iQuoteId=" +Request.QueryString.Get("iQuoteId");
            if(IsPostBack == false)
            {
                sda.Fill(baseDS,"laaf_o.dbo.mergeSegmentDetail");
                DataGrid1.DataBind();



                int loop1;
                // Load NameValueCollection object.
                NameValueCollection coll=Request.QueryString;
                // Get names of all keys into a string array.
                String[] arr1 = coll.AllKeys;
                for (loop1 = 0; loop1 < arr1.Length; loop1++)
                {
                    String[] arr2 = coll.GetValues(arr1[loop1]);
                }
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            foreach(DataGridItem DataGridItem in DataGrid1.Items)
            {
                if(DataGridItem.Cells[0].Controls[1] is CheckBox)
                {
                    CheckBox Checkbox1 = (CheckBox)DataGridItem.Cells[0].Controls[1];
                    if(Checkbox1.Checked == true)
                    {


                        SqlCommand sCmd1 = sc.CreateCommand();
                        SqlDataAdapter sda1 = new SqlDataAdapter();
                        sda1.SelectCommand = sCmd1;
                        sCmd1.CommandType = CommandType.Text;
                        sCmd1.CommandText = "SELECT mergeSegmentDetail.iQuoteId, mergeSegmentDetail.siLineNumber,mergeSegmentDetail .chProductNumber,mergeSegmentDetail.iProductCatId FROM mergeSegmentDetail INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber WHERE laaf_l.dbo.ProductInfo.bIsBaseProduct=1 and mergeSegmentDetail.iQuoteId=" +Request.QueryString.Get("iQuoteId")+ "and mergeSegmentDetail.siLineNumber=" +TextBox1.Text;
                        DataSet baseDS1 = new DataSet();
                        sda1.Fill(baseDS1);

                        DataGridItem.Cells[2].Text = baseDS1.Tables[0].Rows[0].ItemArray[2].ToString();
                        DataGridItem.Cells[3].Text = baseDS1.Tables[0].Rows[0].ItemArray[3].ToString();

                        SqlCommand sCmd2 = sc.CreateCommand();

                        sda.UpdateCommand = sCmd2;
                        sCmd2.CommandType = CommandType.Text;

                        //sCmd2.CommandText = "Update mergeSegmentDetail SET mergeSegmentDetail.iProductCatId ="+baseDS1.Tables[0].Rows[0].ItemArray[2]+",mergeSegmentDetail.chProductNumber ="+baseDS1.Tables[0].Rows[0].ItemArray[3]+ "INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber WHERE laaf_l.dbo.ProductInfo.bIsBaseProduct=1 and iQuoteId ="+Request.QueryString.Get("iQuoteId")+" and siLineNumber="+DataGridItem.Cells[2].Text;
                        sCmd2.CommandText = "Update mergeSegmentDetail SET mergeSegmentDetail.iProductCatId =@iProductCatId,mergeSegmentDetail.chProductNumber =@chproductNumber INNER JOIN laaf_l.dbo.ProductInfo ON mergeSegmentDetail.chProductNumber = laaf_l.dbo.ProductInfo.chProductNumber WHERE laaf_l.dbo.ProductInfo.bIsBaseProduct=1 and iQuoteId ="+Request.QueryString.Get("iQuoteId")+" and siLineNumber="+DataGridItem.Cells[2].Text;
                        sCmd2.Parameters.Add("@iProductCatId",SqlDbType.In t,4,"iProductCatId");
                        sCmd2.Parameters.Add("@chProductNumber",SqlDbType. Int,4,"chProductNumber");
                        sda.Update(baseDS,"mergeSegmentDetail");
                        //sCmd2.ExecuteNonQuery();
                    }
                }

            }
            sda.Fill(baseDS);
            DataGrid1.DataBind();
            sc.Close();
        }
    }
}



 
Old December 5th, 2005, 07:56 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The general process for filling a table is as follows:

  a) Create a SELECT SqlCommand and a SqlDataAdapter

  b) Map the table to SqlDataAdapter

  c) connect the SqlCommand and the SqlDataAdapter

  d) Fill the DataSet

As you error states you need table mappings..
Looking through your code I notice that part b and c are missing.

Take a look at the following example: http://www.c-sharpcorner.com/Database/DBOpsInADOPA.asp

-Adam
 
Old December 6th, 2005, 12:49 PM
Registered User
 
Join Date: Sep 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

B and C are their. The error is not in filling the dataset, the error occurs when I am trying to update the database. Here is B and C

public SqlConnection sc = new SqlConnection("workstation id=QED_IT4;packet size=4096;user id=sa;data source=devsvr1;persist security info=False;initial catalog=laaf_o");
        public DataSet baseDS = new DataSet();
        public SqlDataAdapter sda = new SqlDataAdapter();
        public void Page_Load(object sender, System.EventArgs e)
        {
            sc.Open();// opens the connection
            SqlCommand sCmd = sc.CreateCommand();//assigns the command to the connection string
            sda.SelectCommand = sCmd;//assigns the adapter to the command and connection string
            sCmd.CommandType = CommandType.Text;


 
Old December 6th, 2005, 08:52 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You should use the same SqlAdapter object to Select and Update.
Presently your are selecting with sda1 and updating with sda. You should also specify table mappings (part B).

eg: sda.TableMappings.Add("Table","mergeSegmentDetail" );

Taken from MSDN: "If you do not specify a TableName or a DataTableMapping name when calling the Fill or Update method of the DataAdapter, the DataAdapter will look for a DataTableMapping named "Table". If that DataTableMapping does not exist, the TableName of the DataTable will be "Table". You can specify a default DataTableMapping by creating a DataTableMapping with the name of "Table"."
http://msdn.microsoft.com/library/de...mnmappings.asp

-Adam
 
Old June 23rd, 2007, 06:20 AM
Registered User
 
Join Date: Jun 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The following line should be able to change the exception type.
sda.Update(baseDS,"mergeSegmentDetail");


Jai Guru Dev!
Satish Kumar Suthar





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to find source codes for the book sluggish_homer General .NET 1 May 21st, 2007 09:28 PM
Unable to find a version of the runtime ....... ASPDharm ASP.NET 2.0 Basics 0 January 16th, 2007 07:03 AM
Unable to find dependency (VB.net setup) AlexSu General .NET 0 September 9th, 2005 06:05 AM
Unable to find an entry point named OCIEnvCreate i Twinklestar ADO.NET 2 June 24th, 2004 05:44 AM
Unable to Update phungleon Access ASP 5 March 18th, 2004 05:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.