What is wrong with this code?
There is a message that says,
Update requires a valid InsertCommand when passed DataRow collection with new rows.
How can I fixed this thing?Code:string sql = "select * from tblUserAccount"; SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "tblUserAccount"); DataRow dr = ds.Tables[0].NewRow(); ds.Tables[0].Rows.Add(dr); da.InsertCommand = conn.CreateCommand(); dr.BeginEdit(); dr["fname"] = txtFname.Text; dr["lname"] = txtLname.Text; dr["usernames"] = txtUsername.Text; dr["passwords"] = txtPassword1.Text; dr.EndEdit(); da.Update(ds, "tblUserAccount"); ds.AcceptChanges(); da.Dispose(); conn.Close(); conn.Dispose();
And if I am going to add the highlighted part as shown below the message is:
ExecuteReader: CommandText property has not been initializedCode:Code:string sql = "select * from tblUserAccount"; SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "tblUserAccount"); DataRow dr = ds.Tables[0].NewRow(); ds.Tables[0].Rows.Add(dr); da.InsertCommand = conn.CreateCommand(); dr.BeginEdit(); dr["fname"] = txtFname.Text; dr["lname"] = txtLname.Text; dr["usernames"] = txtUsername.Text; dr["passwords"] = txtPassword1.Text; dr.EndEdit(); da.Update(ds, "tblUserAccount"); ds.AcceptChanges(); da.Dispose(); conn.Close(); conn.Dispose();




Reply With Quote