Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
Hello, I need help. I have a page for adding data in GridView1. After adding data from the textbox, it appears in GridView1 in the last row or column. I want it to appear in the first row.

What I have tried:

VB.NET
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.Configuration
Imports System.Web.Security
Imports System.Collections
Imports System.IO
Public Class AddStudent
    Inherits System.Web.UI.Page
    Protected WithEvents HyperLink2 As System.Web.UI.WebControls.HyperLink
    Protected WithEvents lblOutput As System.Web.UI.WebControls.Label
    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents DropDownList4 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
     Protected WithEvents Label10 As System.Web.UI.WebControls.Label
    Protected WithEvents Textbox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected connStr = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
    Protected WithEvents GridView1 As System.Web.UI.WebControls.GridView
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Textbox2 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label5 As System.Web.UI.WebControls.Label
    Protected WithEvents Label8 As System.Web.UI.WebControls.Label
    Protected WithEvents Label7 As System.Web.UI.WebControls.Label
    Protected WithEvents Textbox3 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label6 As System.Web.UI.WebControls.Label
    Protected WithEvents Textbox4 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox5 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox6 As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
    Protected WithEvents Imagebutton1 As System.Web.UI.WebControls.ImageButton
    Protected Connection = New SqlConnection(connStr)
    Dim PageNo
#Region " Web Form Designer Generated Code "
     'This call is required by the Web Form Designer.
    <system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            PageNo = "5"
            Dim Uname, usertype As String
            Dim Connection As New SqlConnection(connStr)
            Uname = Session.Contents("name")
            Dim ds As New DataSet()
            Dim adapter As New SqlDataAdapter("Select  CONVERT(varchar(20),GETDATE(), 113) as STARTTIME, CONVERT(varchar(20),GETDATE(), 113)  as CLOSETIME ,menuType from [HRusers] Where remarks like '%" & PageNo & "%' and userid = '" & Uname & "'", Connection)
            adapter.Fill(ds, "Details")
           
            If ds.Tables("Details").Rows.Count <> 0 Then

                usertype = ds.Tables("Details").Rows(0).Item("MenuType").ToString()
                'Textbox5.Text = ds.Tables("Details").Rows(0).Item("StartTime").ToString()
                '  Textbox6.Text = ds.Tables("Details").Rows(0).Item("CloseTime").ToString()
            Else
                Response.Redirect("mainpage.aspx")
            End If

        End If
        ' DefaultButton(Me.Textbox4, Me.Button1)
        BindStudent()
    End Sub
 
    Dim ULOCATION, UCOUNTER, ptype
    Dim utype As Char
    Dim Bno
    


    Sub BindStudent()


        Dim Connection As New SqlConnection(connStr)
        Dim adapter As New SqlDataAdapter("SELECT AID AS ACADEMICID, SNAME as STUDENTNAME,building as Department, Email as STATUS,LOCATION,MEALTYPE, CONVERT(varchar(20), STARTTIME, 113) as STARTTIME, CONVERT(varchar(20),endTIME, 113)  as CLOSETIME,CREATEDBY  FROM STUDENT where mobile is null or MOBILE <> 1   order by AID asc", Connection)
        Dim ds As New DataSet()
        adapter.Fill(ds, "tran")
        GridView1.DataSource = ds.Tables("tran").DefaultView
        GridView1.DataBind()
        Label8.Text = DateTime.Now.ToString("hh:mm:ss tt")
        Label7.Text = Date.Now.ToString("M/d/yyyy")


        For i = 0 To GridView1.Rows.Count - 1
            Using sqlCommand As New SqlCommand()

                Label10.Text = GridView1.Rows(0).Cells(0).Text.ToString

            End Using
        Next

    End Sub

  

    'Private Sub Imagebutton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Imagebutton1.Click
    '    Response.Clear()
    '    Response.AddHeader("content-disposition", "attachment;filename=StudentsList.xls")
    '    Response.Charset = ""
    '    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    '    Response.ContentType = "application/vnd.xls"
    '    Dim stringwrite = New System.IO.StringWriter()
    '    Dim htmlwrite = New HtmlTextWriter(stringwrite)
    '    GridView1.RenderControl(htmlwrite)
    '    Response.Write(stringwrite.ToString())
    '    Response.End()
    'End Sub
    Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataBind()
    End Sub
 
End Class
Posted
Updated 27-Dec-23 9:40am
v2
Comments
vblover Programmer 28-Dec-23 1:48am    
You can Using 'ORDER BY' Clause: SELECT - ORDER BY Clause (Transact-SQL)
CHill60 28-Dec-23 7:37am    
As an aside, your code in Page_Load is vulnerable to SQL-Injection attack. Never concatenate strings to create your SQL statements - use parameterised queries instead

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900