Paging Problem in Datagrid...!

Discussion in 'ASP' started by asad_black, Sep 18, 2008.

  1. asad_black

    asad_black New Member

    Joined:
    Jun 3, 2008
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    hi, i have a very small problem in datagrid which is basically contain on the paging problem.

    i create a registration page for new user. my page contain datagrid and a form which contain registration textfields.

    my record insert successfully, but problem is that when i click on the number for view next pages then its shows only registration textfields not a datagrid.

    i dont know what is the problem ....! kindly solve my problem as soon as possible. my code is...!



    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>DataGrid - Insert</title>
    </head>
    <body leftmargin="0" topmargin="0">
    <form id="Form1" method="post" runat="server">
    <table id="Table1"
    style="Z-INDEX: 110; LEFT: 5px; POSITION: absolute; TOP: 5px"
    cellspacing="0" cellpadding="0" width="300" border="0" >
    <tr>
    <td colspan="3">
    <asp:DataGrid id="dgNorthwind" runat="server"
    Width="728" Height="150px" BorderColor="#CC9966"
    BorderStyle="None" BorderWidth="1"
    BackColor="White" CellPadding="2" EnableViewState="False" AllowPaging="True" PageSize="3" OnPageIndexChanged="pager" >
    <PagerStyle Mode="NumericPages" />

    </asp:DataGrid>
    </td>
    </tr>
    <tr>
    <td colSpan="3" style="height: 19px"></td>
    </tr>
    <tr>
    <td colSpan="3" style="height: 148px">
    <p>
    <asp:Label id="Label1" runat="server"
    Width="317" BackColor="Firebrick" ForeColor="White">
    Insert a new record...
    </asp:Label>
    </p>
    <table id="Table2">
    </td>
    </tr>
    <tr>
    <td colSpan="4" style="width: 441px"></td>
    </tr>
    <tr>
    <td style="width: 115px; height: 24px">
    <asp:Label ID="Labelfirstname" runat="server" Text="First Name"></asp:Label></td>
    <td style="height: 24px; width: 277px;"><asp:TextBox id="txtfirstname" runat="server" width="193" /></td>
    <td style="width: 277px; height: 24px">


    <asp:RequiredFieldValidator id="rfvfirstname" runat="server"
    ErrorMessage="Please Enter the First Name"
    ControlToValidate="txtfirstname" /></td>
    </tr>
    <tr>
    <td style="width: 115px; height: 24px">
    <asp:Label ID="Labellastname" runat="server" Text="Last Name"></asp:Label></td>
    <td style="height: 24px; width: 277px;"><asp:TextBox id="txtlastname" runat="server" width="193" /></td>
    <td style="width: 277px; height: 24px">

    <asp:RequiredFieldValidator id="rfvlastname" runat="server"
    ErrorMessage="Please Enter the Last Name"
    ControlToValidate="txtlastname" /></td>
    </tr>
    <tr>
    <td style="WIDTH: 115px; height: 24px;">
    <asp:Label id="Label2" runat="server">Username</asp:Label>
    </td>
    <td style="height: 24px; width: 277px;">
    <asp:TextBox id="txtUserName" runat="server" width="193" />
    </td>
    <td style="width: 277px; height: 24px">


    <asp:RequiredFieldValidator id="rfvUserName" runat="server"
    ErrorMessage="Please Fill the Username"
    ControlToValidate="txtUserName" /></td>
    </tr>
    <tr>
    <td style="WIDTH: 115px">
    <asp:Label id="Label3" runat="server">Password</asp:Label>
    </td>
    <td style="width: 277px">


    <asp:TextBox id="txtPassword" runat="server" width="193" TextMode="Password" /></td>
    <td style="width: 277px">


    <asp:RequiredFieldValidator id="rfvPassword" runat="server"
    ErrorMessage="Please Fill the your desire Password"
    ControlToValidate="txtPassword" /></td>
    </tr>

    <tr>
    <td style="WIDTH: 115px">
    <asp:Label id="Label4" runat="server" Width="118px">Confrim Password</asp:Label>
    </td>
    <td style="width: 277px">


    <asp:TextBox id="txtconfirmpass" runat="server" width="193" TextMode="Password" /></td>
    <td style="width: 277px">

    <asp:CompareValidator
    id="compval"
    Display="dynamic"
    ControlToValidate="txtPassword"
    ControlToCompare="txtconfirmpass"
    ForeColor="red"
    BackColor="yellow"
    Type="String"
    EnableClientScript="false"
    Text="Your Password Does Not Match"
    runat="server" Width="271px" /></td>
    </tr>


    <tr>
    <td style="WIDTH: 115px" colSpan="2"><br />
    <asp:Button id="btnInsert" runat="server"
    OnClick="btnInsert_Click" width="298" text="INSERT!" />
    </td>
    <td colspan="1" style="width: 115px">
    <asp:Label ID="msgofaddusername" runat="server" Width="238px"></asp:Label></td>
    </tr>
    </table>




    </form>


    </body>
    </html>

    <script runat="server">
    Dim objConnection As OleDbConnection
    Dim daNorthwind As OleDbDataAdapter
    Dim dsNorthwind As DataSet

    Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
    objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _
    & "DATA SOURCE=" _
    & Server.MapPath("nwind.mdb;"))

    Dim strSQL As String = "SELECT firstname, lastname, username, pw " & _
    "FROM login"
    daNorthwind = New OleDbDataAdapter(strSQL, objConnection)

    ' Create a command builder object in order to create
    ' INSERT, UPDATE, and DELETE SQL statements automatically
    Dim cb As New OleDbCommandBuilder(daNorthwind)

    ' Is the page being loaded for the first time?
    If Not Page.IsPostBack Then
    FillDataGrid()
    End If
    End Sub
    Sub FillDataGrid()

    ' Create a new dataset to contain categories' records
    dsNorthwind = New DataSet()

    ' Fill the dataset retrieving data from the database
    daNorthwind.Fill(dsNorthwind)

    ' Set the DataSource property of the DataGrid
    dgNorthwind.DataSource = dsNorthwind.Tables(0).DefaultView

    ' Bind the dataset data to the DataGrid
    dgNorthwind.DataBind()
    End Sub



    Sub pager(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
    dgNorthwind.CurrentPageIndex = e.NewPageIndex

    FillDataGrid()
    End Sub

    Sub btnInsert_Click(ByVal Sender As Object, ByVal E As EventArgs)


    ' If user has filled every text box correctly...
    If Page.IsValid Then

    ' Create a temporary dataset to contain the new record
    Dim dsTemp As New DataSet()

    ' Fill the temporary dataset
    daNorthwind.Fill(dsTemp)
    ' Create a new row
    Dim r As DataRow = dsTemp.Tables(0).NewRow()

    ' Add the category name, reading its value from the text box
    r("firstname") = txtfirstname.Text

    ' Add the category description, reading its value from the text box
    r("lastname") = txtlastname.Text

    ' Add the category name, reading its value from the text box
    r("username") = txtUserName.Text

    ' Add the category description, reading its value from the text box
    r("pw") = txtPassword.Text

    ' Add the new row into the dataset's rows collection
    dsTemp.Tables(0).Rows.Add(r)

    ' Update the database using the temporary dataset
    daNorthwind.Update(dsTemp)

    ' Usually, you have to call the AcceptChanges() method in order to align the
    ' dataset with records in the database. Because this is a temporary dataset,
    ' we can omit this instruction.
    dsTemp.AcceptChanges()

    ' Refresh the data grid to display the new record
    FillDataGrid()

    msgofaddusername.Text = "New account of " + txtfirstname.Text + " " + txtlastname.Text + " has been created successfully"


    End If
    End Sub





    </script>
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice