Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 25th, 2006, 11:38 AM
Authorized User
 
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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:



 
Old January 25th, 2006, 01:39 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

It shouldn't matter, but you don't need to manually open the connection before you fill from a data adapter. The adapter call will handle it.

Can you look at the data returned and see why it thinks there isn't any?

-Peter
 
Old January 26th, 2006, 05:21 AM
Authorized User
 
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks planoie, problem solved! thanks alot






Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting rows in Winforms Datagrid livehed General .NET 0 May 4th, 2006 01:23 AM
problem in deleting the row from datagrid swati_joshi ASP.NET 1.0 and 1.1 Basics 10 March 24th, 2006 11:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.