|
-
Jul 26th, 2005, 03:20 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Delete from datagrid
I am currently trying to delete a row from the datagrid, so far so good until I tried to "acceptchanges" and keep on getting this error message:
"Update requires the DeleteCommand to have a Connection object. The Connection property of the DeleteCommand has not been initialized"
This is my code:
Try
Dim dt_BussLine As DataTable = Me.dg_BusLine.DataSource
dt_BussLine.Rows(dg_BusLine.CurrentRowIndex).Delete()
dadaptBusLine.Update(DsBusLine1)
Me.DsBusLine1.AcceptChanges()
Catch eexception As Exception
MsgBox(eexception.Message)
End Try
I don't understand what it means since I am currently connected to the table. Please Help!
-
Jul 26th, 2005, 03:28 AM
#2
Re: Delete from datagrid
you didn't pass a connection to your delete command.
-
Jul 26th, 2005, 03:32 AM
#3
Thread Starter
Addicted Member
Re: Delete from datagrid
sorry for sounding stupid since i'm new at this, but how do i do that? i though passing a connection to the form itself is enough.
-
Jul 26th, 2005, 03:41 AM
#4
Re: Delete from datagrid
could you post your delete command
in passing a connection to your command
VB Code:
'cn is your connection
Dim da As New SqlDataAdapter("delete from table where id='" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0) & "'", [b]cn[/b])
-
Jul 26th, 2005, 08:48 PM
#5
Thread Starter
Addicted Member
Re: Delete from datagrid
This my whole Delete command (I have a feeling I missed something)
Private Sub btn_Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Delete.Click
Dim strAns As String
strMode = "Delete"
strAns = MsgBox("Are you sure you want to delete the selected row?", MsgBoxStyle.YesNo)
If strAns = "6" Then
Try
Dim dt_BussLine As DataTable = Me.dg_BusLine.DataSource
dt_BussLine.Rows(dg_BusLine.CurrentRowIndex).Delete()
dadaptBusLine.Update(DsBusLine1)
Me.DsBusLine1.AcceptChanges()
Catch eexception As Exception
MsgBox(eexception.Message)
End Try
End If
End Sub
-
Jul 26th, 2005, 09:10 PM
#6
Re: Delete from datagrid
try this one out
VB Code:
Dim dt_BussLine As DataTable = Me.dg_BusLine.DataSource
dt_BussLine.Rows(dg_BusLine.CurrentRowIndex).Delete()
dim cm as new sqlcommandbuilder(dadaptBusLine)
dadaptBusLine.Update(DsBusLine1)
-
Jul 26th, 2005, 09:14 PM
#7
Thread Starter
Addicted Member
Re: Delete from datagrid
same error message I'm afraid :
"Update requires the DeleteCommand to have a Connection object. The Connection property of the DeleteCommand has not been initialized"
-
Jul 26th, 2005, 09:45 PM
#8
Re: Delete from datagrid
if that's the case then there is something wrong w/ your dataadapter and your connection.
does the dadaptBusLine is the one you use in filling your datatable and binding it to datagrid?
-
Jul 26th, 2005, 09:54 PM
#9
Thread Starter
Addicted Member
Re: Delete from datagrid
uh-oh...databind?
is this code databinding it to the datagrid?
Private Sub frm_BusLine_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim frm_Parent As frm_SRMAINSCRN
dim dadaptBusLine As New SqlClient.SqlDataAdapter("select buss_code, buss_desc from cde_buss_line", conBusLine)
Me.Text = "Business LIne"
Me.txt_BusLine.ReadOnly = True
Me.txt_BusDesc.ReadOnly = True
Me.btn_Save.Enabled = False
Me.btn_Cancel.Enabled = False
strMode = "Initial"
dadaptBusLine.Fill(DsBusLine1, "CDE_Buss_Line")
Dim dgts_BusLine As New DataGridTableStyle
dgts_BusLine.MappingName = "CDE_BUSS_LINE"
Dim acol1 As New DataGridTextBoxColumn
Dim acol2 As New DataGridTextBoxColumn
acol1.MappingName = "Buss_Code"
acol1.HeaderText = "Business Code"
acol1.Width = 113
dgts_BusLine.GridColumnStyles.Add(acol1)
acol2.MappingName = "Buss_Desc"
acol2.HeaderText = "Description"
acol2.Width = 200
dgts_BusLine.GridColumnStyles.Add(acol2)
Me.dg_BusLine.TableStyles.Add(dgts_BusLine)
RowNum = Me.dg_BusLine.CurrentRowIndex
Me.txt_BusLine.Text = dg_BusLine(RowNum, 0)
Me.txt_BusDesc.Text = dg_BusLine(RowNum, 1)
End Sub
I guess to make things easier for both of us. Maybe you can give me steps (not necessarily code though codes would be great) on how to delete a row from the datagrid and then apply that deletion to the table itself.
-
Jul 26th, 2005, 10:11 PM
#10
Re: Delete from datagrid
ok this one.
it works fine in my machine
VB Code:
'cn is a connection
Dim ds As New DataSet()
Dim da As New SqlDataAdapter("select * from test", cn)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ds.Tables.Add("dt")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
da.Fill(ds.Tables("dt"))
DataGrid1.DataSource = ds.Tables("dt")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim d As DataRow = ds.Tables("dt").Rows(DataGrid1.CurrentRowIndex)
d.Delete()
Dim cmb As New SqlCommandBuilder(da)
da.Update(ds.Tables("dt"))
MessageBox.Show("Data has been updated")
Catch ex As Exception
MessageBox.Show("You must select first a row")
End Try
End Sub
-
Jul 26th, 2005, 10:29 PM
#11
Thread Starter
Addicted Member
Re: Delete from datagrid
sorry didn't work. but it worked when i added :
dadaptBusLine.DeleteCommand = New SqlClient.SqlCommand("delete from cde_buss_line where buss_code ='" & Me.txt_BusLine.Text & "'", conBusLine)
what do you think? i'm afraid that it might have some hidden repurcussions that i don't know about.
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
|