Waqas Ali

Waqas Ali

  • NA
  • 1
  • 779

Invalid postback argument when GridView1_RowCommand fired

Mar 6 2015 2:11 AM


Imports System.Data.SqlClient
Public Class listview
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Response.Write("value loop " & gvwExample.PageIndex & "</br>")
        
        Dim cmd As SqlCommand = New SqlCommand("SELECT  UserName,pass_word,ApplicantName,FatherName FROM userinfo", New SqlConnection(" Server=vu-it-wqs-pc\sqlexpress;Database=Test;Uid=sa;Pwd=123;"))
        Try
            Dim adp As New SqlDataAdapter(cmd)
            cmd.Connection.Open()
            Dim ds As New DataSet()
            adp.Fill(ds)
            gvwExample.DataSource = ds
            cmd.ExecuteReader()

            gvwExample.DataBind()
            adp.Dispose()
            ds.Dispose()

            cmd.Connection.Dispose()

        Catch ex As Exception
            lblStatus.Text = ex.Message
       


        End Try
    End Sub
    Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles gvwExample.PageIndexChanging
        Try
            gvwExample.PageIndex = e.NewPageIndex
            gvwExample.AllowPaging = True

            gvwExample.DataBind()
        Catch ex As Exception
            lblStatus.Text = ex.Message
        End Try
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
        Response.Redirect("DBform.aspx")
        lblStatus.Text = "dddd"
        If Not (IsPostBack) Then

            lblStatus.Text = "dddd1"
            If (e.CommandName = "Delete") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                lblStatus.Text = "dddd2"
                ' Retrieve the row that contains the button 
                ' from the Rows collection.
                Dim row As GridViewRow = gvwExample.Rows(index)
                Response.Write("value loop " + index + "</br>")
                ' Add code here to add the item to the shopping cart.

            End If
        End If
    End Sub



    'Protected Sub gvwExample_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gvwExample.SelectedIndexChanged
    '    If Not (IsPostBack) Then


    '        'If (e.CommandName = "Delete") Then
    '        '    ' Retrieve the row index stored in the CommandArgument property.
    '        '    Dim index As Integer = Convert.ToInt32(e.CommandArgument)

    '        '    ' Retrieve the row that contains the button 
    '        '    ' from the Rows collection.
    '        '    Dim row As GridViewRow = gvwExample.Rows(index)
    '        Response.Write("value loop 55" & gvwExample.PageIndex & "</br>")
    '        ' Add code here to add the item to the shopping cart.

    '        'End If
    '    End If
    'End Sub
End Class






aspx code


 <%@ Page Language="vb" AutoEventWireup="false"  CodeBehind="listview.aspx.vb" Inherits="WebAppFramework20.listview" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .basix {}
    </style>
</head>
<body>
  
    <form id="form1" runat="server">
   <table style="width:100%">
             
             <tr>
                <td>
                    <img alt="HTML5 Icon" src="images/D1.jpg" style="width:100px;height:50px" /></td>
                <td>
                    <img alt="HTML5 Icon" src="images/D2.png" style="width:600px;height:10px" /></td>
                <td>
                    <img alt="HTML5 Icon" src="images/D3.png" style="width:80px;height:80px" /></td>
            </tr>
        </table>
       
             <table style="width:50%">
             
             <tr>
                <td style="width:10px;height:10px" >
                    aaaa
                </td> <td style="width:10px;height:10px" >
                    aaaab
                </td> <td style="width:10px;height:10px" >
                    aaaac
                </td>
                
            </tr>
        </table>
       <br /> <br />
        <asp:GridView ID="gvwExample" runat="server"   CssClass="basix"  
             CellPadding="4" EnableModelValidation="True" ForeColor="Black"
             GridLines="Horizontal" AutoGenerateColumns="False" BackColor="White"
             BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Width="666px"
             PageSize="5"  AllowPaging="True" OnPageIndexChanging="OnPaging">
           
<Columns >
    

<asp:BoundField DataField="UserName" HeaderText="First Name" />
<asp:BoundField DataField="pass_word" HeaderText="Last Name" />
<asp:BoundField DataField="ApplicantName" HeaderText="Date Hired" />
    <asp:BoundField DataField="FatherName" HeaderText="First Name" />
    <asp:HyperLinkField HeaderText="View Details..." Text="View Details..." DataNavigateUrlFields="UserName"
            DataNavigateUrlFormatString="DBform.aspx?ID={0}" />      
    <asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="Delete" runat="server" 
         CommandName = "Delete"
    CommandArgument = "<%# CType(Container,GridViewRow).RowIndex %>"
      Text="Delete" />
  </ItemTemplate> 
</asp:TemplateField>
             </Columns> 
               
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                
            </asp:GridView>
              <asp:Label ID="lblStatus" runat="server" Text="" Width="300px" style="margin-left: 20px"></asp:Label>

    </form>
</body>
</html>




i found this error when row command button event fire


Attachment: webappframework20.zip