|
-
May 8th, 2007, 09:35 AM
#1
Thread Starter
Addicted Member
[2.0] problem with insert command
What is wrong with this code?
There is a message that says,
Update requires a valid InsertCommand when passed DataRow collection with new rows.
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();
How can I fixed this thing?
And if I am going to add the highlighted part as shown below the message is:
ExecuteReader: CommandText property has not been initialized
Code:
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();
-
May 8th, 2007, 06:21 PM
#2
Re: [2.0] problem with insert command
This line:
C# Code:
da.InsertCommand = conn.CreateCommand();
simply creates an SqlCommand object configured to use that connection. It doesn't generate any SQL code or create any paramaters. How could it? The connection doesn't know anything about the data that the command will be used against, nor does it know whether the command will be used to execute a SELECT statement, an INSERT statement, an UPDATE statement, or something else entirely. It's up to you to assign the appropriate SQL code to the CommandText property and to add any required parameters.
-
May 8th, 2007, 08:32 PM
#3
Thread Starter
Addicted Member
Re: [2.0] problem with insert command
Do we have a bindingContext in c#?
I Like this code in VB:
Code:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Country = cboCountry.Text
Equipment = cboEquipment.Text
Cost = txtCostPerEquip.Text
chkGen = txtChkGen.Text
'get new row in the dataset
Dim conn As OleDb.OleDbConnection = GetConnection()
Dim sql As String = "Select * from tblVolGenDiscount where DiscountID = -1"
Dim sa As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)
Dim ds As New DataSet
'create new row
sa.Fill(ds, "tblVolGenDiscount")
Dim dr As DataRow = ds.Tables(0).NewRow
ds.Tables(0).Rows.Add(dr)
Binder(ds)
cboCountry.Text = Country
cboEquipment.Text = Equipment
txtCostPerEquip.Text = Cost
txtDocID.Text = g_docid
txtChkGen.Text = chkGen
If cboCountry.Text = "" Then
MsgBox("Please enter Country.", MsgBoxStyle.OkOnly)
cboCountry.Focus()
GoTo ReturnError
End If
If cboEquipment.Text = "" Then
MsgBox("Please enter Equipment.", MsgBoxStyle.OkOnly)
cboEquipment.Focus()
GoTo ReturnError
End If
If Me.txtChkGen.Text = "" Then
Me.txtChkGen.Text = "no"
End If
If txtCostPerEquip.Text = "" Then
txtCostPerEquip.Text = "0.00"
End If
If Me.txtCostPerEquip.Text.Contains("%") = True Then
'do nothing
Else
If IsNumeric(Me.txtCostPerEquip.Text) Then
Me.txtCostPerEquip.Text = Me.txtCostPerEquip.Text & " %"
Else
Me.txtCostPerEquip.Text = Me.txtCostPerEquip.Text
End If
End If
BindingContext(dsEditDS, "tblVolGenDiscount").EndCurrentEdit()
Dim sqlWrite As String = "Select * from tblVolGenDiscount"
Dim daWrite As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sqlWrite, conn)
Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(daWrite)
If ds.HasChanges Then
daWrite.Update(ds, "tblVolGenDiscount")
ds.AcceptChanges()
End If
Me.cboCountry.DataBindings.Clear()
Me.cboEquipment.DataBindings.Clear()
Me.txtCostPerEquip.DataBindings.Clear()
Me.txtDocID.DataBindings.Clear()
Me.txtChkGen.DataBindings.Clear()
'Me.cboCountry.Text = ""
'Me.cboEquipment.Text = ""
'Me.txtCostPerEquip.Text = ""
'Me.txtChkGen.Text = ""
'Me.chkGenDiscount.Checked = False
Me.LoadData()
ReturnError:
Me.cboCountry.DataBindings.Clear()
Me.cboEquipment.DataBindings.Clear()
Me.txtCostPerEquip.DataBindings.Clear()
Me.txtDocID.DataBindings.Clear()
Me.txtChkGen.DataBindings.Clear()
End Sub
and for the binding thing:
Code:
Public Sub Binder(ByVal ds As DataSet)
dsEditDS = ds
Me.cboCountry.DataBindings.Add(New Binding("Text", ds, "tblVolGenDiscount.Country"))
Me.cboEquipment.DataBindings.Add(New Binding("Text", ds, "tblVolGenDiscount.Equipment"))
Me.txtCostPerEquip.DataBindings.Add(New Binding("Text", ds, "tblVolGenDiscount.CostPerEquipType"))
Me.txtDocID.DataBindings.Add(New Binding("Text", ds, "tblVolGenDiscount.ContractID_fk"))
Me.txtChkGen.DataBindings.Add(New Binding("Text", ds, "tblVolGenDiscount.GenDiscount"))
End Sub
Can we do this in c#?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|