Update unable to find TableMapping['Table'] or DataTable 'Table' in c# sqlserver

Discussion in 'Software' started by Nishanth, Nov 9, 2009.

  1. Nishanth

    Nishanth

    Joined:
    Nov 9, 2009
    Messages:
    1
    Likes Received:
    0
    Hi i am trying to update my sql server table with dataAdapter.Update('datasetname')

    Here i am getting the exception: " Update unable to find TableMapping['Table'] or DataTable 'Table' ".

    My C# code is as follows :

    public int SaveSalesDetails(Int64 customerId,Int64 appTypeId,string purchaseOrderNo,float VAT,string modifiedBy,string vehicleNo,float lessAmount,float totalAmount,int paymentStatus,
    int modeOfPayment, float cashAmount, string chequeNo, string bank, string chequeDate, float chequeAmount, DataSet dsItems)
    {
    SqlTransaction trans = null;
    int retVal = 0;
    Int64 newSalesInvoiceNo=0;
    DataSet dsItemsNew;
    DataTable dtItemsNew;
    try
    {
    connection = new SqlConnection(ConfigurationManager.AppSettings.Get("conString"));
    connection.Open();
    trans = connection.BeginTransaction();

    //insert into tblSales table
    string sql = " INSERT INTO tblSales (nCustomerId,nAppTypeId,vPurchaseOrder,fVAT,dSaleDate,vModifiedBy,dModifiedDate,vVehicleNumber,fLess,fTotal,nPaymentStatus,nModeOfPayment,CashAmount,ChequeNo,Bank,CheckDate,ChequeAmount) VALUES";
    sql += "(" + customerId + "," + appTypeId + ",'" + purchaseOrderNo + "'," + VAT + ",'" + DateTime.Today.Date + "' , '" + modifiedBy + "','" + DateTime.Today.Date + "' ,'" + vehicleNo + "'," + lessAmount + "," + totalAmount + "," + paymentStatus + "," + modeOfPayment + "," + cashAmount + ",'" + chequeNo + "','" + bank + "','" + chequeDate + "','" + chequeAmount + "') ;";
    sql += " SELECT SCOPE_IDENTITY()";

    command = new SqlCommand(sql, connection);
    command.Transaction = trans;
    newSalesInvoiceNo = Convert.ToInt64(command.ExecuteScalar());

    if (newSalesInvoiceNo > 0)
    {

    //insert into Kesbro_tblSalesDetails table

    //create new column for sales invoice number
    int rown = dsItems.Tables[0].Rows.Count;
    dtItemsNew = dsItems.Tables[0].Copy();
    int chk2 = dtItemsNew.Rows.Count;
    string check1 = dtItemsNew.Rows[0]["vSerialNumber"].ToString();


    DataColumn dtcol1= new DataColumn("vSalesInvoiceNumber",Type.GetType("System.Int64"));
    dtItemsNew.Columns.Add(dtcol1);

    foreach (DataRow row in dtItemsNew.Rows)
    {
    row["vSalesInvoiceNumber"] = newSalesInvoiceNo;
    }
    dtItemsNew.AcceptChanges();

    dsItemsNew = new DataSet();
    dsItemsNew.Tables.Add(dtItemsNew);


    //do dataset insert into tblSalesDetails table
    dataAdapter = new SqlDataAdapter(command);
    string sqlcmdTxt = "INSERT INTO tblSalesDetails(vSalesInvoiceNumber,nBatteryId,vSerialNumber,fUnitPrice,nQuantity,fAmount) VALUES "+
    "(@vSalesInvoiceNumber,@nBatteryId,@vSerialNumber,@fUnitPrice,@nQuantity,@fAmount )";

    dataAdapter.InsertCommand = new SqlCommand(sqlcmdTxt, connection);

    dataAdapter.InsertCommand.Parameters.Add("@vSalesInvoiceNumber", SqlDbType.BigInt, 0, "vSalesInvoiceNumber");
    dataAdapter.InsertCommand.Parameters.Add("@nBatteryId", SqlDbType.BigInt, 0, "nBatteryId");
    dataAdapter.InsertCommand.Parameters.Add("@vSerialNumber", SqlDbType.BigInt, 0, "vSerialNumber");
    dataAdapter.InsertCommand.Parameters.Add("@fUnitPrice", SqlDbType.BigInt, 0, "fUnitPrice");
    dataAdapter.InsertCommand.Parameters.Add("@nQuantity", SqlDbType.BigInt, 0, "nQuantity");
    dataAdapter.InsertCommand.Parameters.Add("@fAmount", SqlDbType.BigInt, 0, "fAmount");

    int ret = dataAdapter.Update(dsItemsNew);
    if (ret > 0)
    {
    retVal = 1;
    }
    }

    trans.Commit();

    return retVal;
    }
    catch (Exception ex)
    {
    trans.Rollback();
    return 0;
    }
    }



    Here the exception is comming while esxcecuting the line
    " int ret = dataAdapter.Update(dsItemsNew); " before that line every thing ws fine.


    Somebody pls help. Very Urgent
     
    Last edited: Nov 9, 2009
    Nishanth, Nov 9, 2009
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.