Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Can anybody help me?
I used the following code for inserting to one table and to update the field by setting name as primarykey on-another table . On debuging if i select a name to display the content to ultra grid it show the exception like ... 'Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information'. ... "my code is marked below:
C#
private void button2_Click(object sender, EventArgs e)
{

    DataTable dts = new DataTable();
    int u;
    u = dt.DefaultView.Table.Rows.Count;
    SqlDataAdapter da1 = new SqlDataAdapter();
    //DataTable dts = new DataTable();
    connect c = new connect();
    SqlConnection s1 = c.con1();
    //DataSet ds = new DataSet("electrical");
    SqlCommand cm = new SqlCommand("select BILLNO,NAME,CONTRACTOR,SITE, COMPANY,PRODUCT,PRODUCTTYPE,QTY,MRP,RATE,AMT,[DISCOUNT%],TOTAL ,DATE from credit", con);
    da1.SelectCommand = cm;
    SqlCommandBuilder cmbuilder = new SqlCommandBuilder(da1);//auto generate the UpdateCommand,InsertCommand and DeleteCommand
    dts.Clear();
    //dt.Clear();
    da1.Fill(dts);
    if (comboBox5.Text == "")
    {
        comboBox5.Text = "";
    }
    if (comboBox4.Text == "")
    {
        comboBox4.Text = "";
    }
    if (comboBox6.Text == "")
    {
        comboBox6.Text = "";
    }
    DataRow row = null;
    DataColumn column1 = new DataColumn("BILLNO", typeof(int));
    dt.Columns.Add(column1);
    DataColumn column14 = new DataColumn("DATE", typeof(DateTime));
    dt.Columns.Add(column14);
    DataColumn column2 = new DataColumn("NAME", typeof(string));
    dt.Columns.Add(column2);
    DataColumn column3 = new DataColumn("CONTRACTOR", typeof(string));
    dt.Columns.Add(column3);
    DataColumn column4 = new DataColumn("SITE", typeof(string));
    dt.Columns.Add(column4);


    for (int i = 1; i <= u; i++)
    {
        row = dt.Rows[i - 1];
        row["BILLNO"] = int.Parse(textBox1.Text);
        row["DATE"] = dateTimePicker1.Value.ToShortDateString();
        row["NAME"] = comboBox5.Text;
        row["CONTRACTOR"] = comboBox4.Text;
        row["SITE"] = comboBox6.Text;
    }

    this.ultraGrid1.BindingContext[dts].EndCurrentEdit();
    this.ultraGrid1.PerformAction(UltraGridAction.DeleteRows);
    dts = dt;
    ////
    //DataSet dst = new DataSet();
    //da1.InsertCommand = cmbuilder.GetInsertCommand();
    ////da1.UpdateCommand = cmbuilder.GetUpdateCommand();
    //da1.Update(dst.Tables[0].Select(null, null, DataViewRowState.CurrentRows));

    ////DataSet ds2 = new DataSet("electricals");
    da1.Update(dts);
    //da1.Update(dst.Tables["credit"].Select(null, null, DataViewRowState.CurrentRows));//, "credit");


    dt.Columns.Remove(column1);
    dt.Columns.Remove(column14);
    dt.Columns.Remove(column2);
    dt.Columns.Remove(column3);
    dt.Columns.Remove(column4);
    MessageBox.Show("Updated");
    // dt.Clear();
    // dts.Clear();

    this.BeginInvoke(new MethodInvoker(this.SetActiveCell));

    try
    {
        if (comboBox5.Text == "")
        {
            comboBox5.Text = "";
        }
        if (comboBox4.Text == "")
        {
            comboBox4.Text = "";
        }
        if (comboBox6.Text == "")
        {
            comboBox6.Text = "";
        }
        connect c1 = new connect();
        c1.idu("insert into creditcustomer values(" + textBox1.Text + ",'" + comboBox5.Text + "','" + dateTimePicker1.Value.ToShortDateString() + "','" + comboBox4.Text + "','" + comboBox6.Text + "'," + textBox7.Text + "," + textBox12.Text + "," + textBox2.Text + "," + textBox12.Text + ")");
        MessageBox.Show("inserted");
        connect cc4 = new connect();
        DataTable dtt4 = cc4.select(" select COMPANY,PRODUCT,RATE,QTY,AMT ,TOTAL from credit where BILLNO=" + textBox1.Text + "");
        dgv.DataSource = dtt4;
    }
    catch (Exception)
    {
        connect cc = new connect();
        cc.idu(" update creditcustomer set BILLNO=" + textBox1.Text + ",DATE='" + dateTimePicker1.Value.ToShortDateString() + "',TOTALAMOUNT=" + textBox7.Text + ",CURRENTBALANCE= " + textBox12.Text + ", PAID = " + ptxt17.Text + ", BALANCE=" + textBox12.Text + " where NAME='" + comboBox5.Text + "'");
        MessageBox.Show("ITEMS ADDED");
        connect c4 = new connect();
        DataTable dt4 = c4.select(" select CONTRACTOR,SITE,CONVERT (VARCHAR(10),DATE,103) AS DATE, TOTALAMOUNT from creditcustomer where BILLNO=" + textBox1.Text + "");
        dgv.DataSource = dt4;

    }
}

How can I avoid the error? Please help me.

Thanks in advance
Posted
Updated 31-Jan-11 11:12am
v4
Comments
m@dhu 31-Jan-11 1:00am    
The error mostly occurs when you don't define a primary key in your database table.

1 solution

Seems to me that your table is missing a primary key. Unlike Insert it you update you need a primary key.

Good luck
 
Share this answer
 

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