insert record and shows in datagrid with paging problem , db Adelaide, Australia
  • 12 years ago


     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="" >
        <title>DataGrid - Insert</title>
      <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" >
              <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" />
              <td colSpan="3" style="height: 19px"></td>
              <td colSpan="3" style="height: 148px">
                  <asp:Label id="Label1" runat="server"
                             Width="317" BackColor="Firebrick" ForeColor="White">
                    Insert a new record...
          <table id="Table2">
              <td colSpan="4" style="width: 441px"></td>
              <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>
                  <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>
              <td style="WIDTH: 115px; height: 24px;">
                <asp:Label id="Label2" runat="server">Username</asp:Label>
              <td style="height: 24px; width: 277px;">
                <asp:TextBox id="txtUserName" runat="server" width="193" />
                <td style="width: 277px; height: 24px">
          <asp:RequiredFieldValidator id="rfvUserName" runat="server"
              ErrorMessage="Please Fill the Username"
              ControlToValidate="txtUserName" /></td>
              <td style="WIDTH: 115px">
                <asp:Label id="Label3" runat="server">Password</asp:Label>
              <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>
              <td style="WIDTH: 115px">
                <asp:Label id="Label4" runat="server" Width="118px">Confrim Password</asp:Label>
              <td style="width: 277px">
                <asp:TextBox id="txtconfirmpass" runat="server" width="193" TextMode="Password" /></td>
                <td style="width: 277px">
    Text="Your Password Does Not Match"
    runat="server" Width="271px" /></td>
              <td style="WIDTH: 115px" colSpan="2"><br />
                <asp:Button id="btnInsert" runat="server"
                     OnClick="btnInsert_Click" width="298" text="INSERT!" />
                <td colspan="1" style="width: 115px">
                    <asp:Label ID="msgofaddusername" runat="server"  Width="238px"></asp:Label></td>


    <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
            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

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

            ' Bind the dataset data to the DataGrid
        End Sub

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

        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
                ' 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

                ' Update the database using the temporary dataset

                ' 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.

                ' Refresh the data grid to display the new record
                msgofaddusername.Text = "New account of " + txtfirstname.Text + " " + txtlastname.Text + " has been created successfully"
            End If
        End Sub


  • 12 years ago


    First remove this "EnableViewState="False" and test it.

    Good Luck


    Hari K......

  • 12 years ago

    thanks alot my friend....!



Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”