deleting in datagrid
hi, guys i have some errors. any ideas..? thanks alot
<asp:datagrid id="dgEmps" runat="server" Height="190px" DataKeyField="Id" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Delete">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<ASP:CHECKBOX id="chkdel" Runat="server" />
</ItemTemplate>
<FooterStyle VerticalAlign="Middle"></FooterStyle>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn DataField="Id" ReadOnly="True" HeaderText="Employee ID">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="sender" HeaderText="Employee First Name">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Subject" HeaderText="Employee Second Name">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:datagrid><asp:button id="btndelete" style="Z-INDEX: 101; LEFT: 312px; POSITION: absolute; TOP: 296px" runat="server" Text="Button"></asp:button>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
Dim chkSelected As System.Web.UI.WebControls.CheckBox
Dim chkDel As DataGridItem
conn = New SqlConnection( _
"server=gorilla;uid=sa;pwd=passStudent; " & _
"database=Stardeveloper")
For Each chkDel In dgEmps.Items
Dim strSQL As String = ""
chkSelected = chkDel.FindControl("chkdel")
If chkSelected.Checked Then
'get the primary key of the row to delete
Dim del_empID As Integer = Convert.ToInt32(chkDel.Cells(2).Text)
strSQL = "DELETE FROM Messages WHERE Id=" & del_empID & ";"
'setting the connection and query details
conn = New SqlConnection( _
"server=gorilla;uid=sa;pwd=passStudent; " & _
"database=Stardeveloper")
Dim myCommand As New SqlCommand(strSQL, conn)
conn.Open()
Dim myDA As New SqlDataAdapter
myDA.SelectCommand = myCommand
Dim myDS As New DataSet
myDA.Fill(myDS)
dgEmps.DataSource = myDS
dgEmps.DataBind()
End If
Next
'save the update to the database and show them on the screen
dgEmps.EditItemIndex = -1
BindData()
End Sub
Private Sub BindData()
'Create a connection
conn = New SqlConnection( _
"server=gorilla;uid=sa;pwd=passStudent; " & _
"database=Stardeveloper")
'Create the command object, passing in the SQL string
Const strSQL As String = "select Id, Sender, Subject, Date from Messages"
Dim myCommand As New SqlCommand(strSQL, conn)
conn.Open()
'Create the DataAdapter
Dim myDA As New SqlDataAdapter
myDA.SelectCommand = myCommand
'Populate the DataSet
Dim myDS As New DataSet
myDA.Fill(myDS)
'Set the datagrid's datasource to the dataset and databind
dgEmps.DataSource = myDS
dgEmps.DataBind()
End Sub
The error occurs here
Exception Details: System.Web.HttpException: The IListSource does not contain any data sources.
Source Error:
Line 72:
Line 73: dgEmps.DataSource = myDS
Line 74: dgEmps.DataBind()
Line 75:
Line 76:
|