How to use Datagrid in ASP.NET?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How to use Datagrid in ASP.NET?

  1. #1
    Hema Guest

    How to use Datagrid in ASP.NET?


    Data should be pulled from database and displayed in datagrid. I prefer to
    write code in aspx.vb instead of aspx. Can you please provide me code.


  2. #2
    Raghu Guest

    Re: How to use Datagrid in ASP.NET?


    "Hema" <hemakumar65@hotmail.com> wrote:
    >
    >Data should be pulled from database and displayed in datagrid. I prefer

    to
    >write code in aspx.vb instead of aspx. Can you please provide me code.
    >

    this is the code write in page load


    Dim Conn As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    Dim Sqlstr As String

    Sqlstr = "select * from emp"
    Dim myCommand As New SqlCommand(Sqlstr, Conn)

    myCommand.CommandType = CommandType.Text
    Conn.Open()


    Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dataGrid1.DataSource = result
    dataGrid1.DataBind()



  3. #3
    Hema Guest

    Re: How to use Datagrid in ASP.NET?


    Hello Raghu

    datatypes SqlConnection, SqlCommand,SqlDataReader are not available in my
    asp.net. How to fix it? otherwise can you provide me another syntax?

    Thank you

    "Raghu" <reach_raghu@mailfromx.com> wrote:
    >
    >"Hema" <hemakumar65@hotmail.com> wrote:
    >>
    >>Data should be pulled from database and displayed in datagrid. I prefer

    >to
    >>write code in aspx.vb instead of aspx. Can you please provide me code.
    >>

    >this is the code write in page load
    >
    >
    >Dim Conn As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    >
    > Dim Sqlstr As String
    >
    > Sqlstr = "select * from emp"
    > Dim myCommand As New SqlCommand(Sqlstr, Conn)
    >
    > myCommand.CommandType = CommandType.Text
    > Conn.Open()
    >
    >
    > Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > dataGrid1.DataSource = result
    > dataGrid1.DataBind()
    >
    >



  4. #4
    Raghu Guest

    Re: How to use Datagrid in ASP.NET?


    all data types are available in asp.net
    you have to import name sapaces..
    Iam sending two files try it.

    ################
    File Name Webform2.aspx
    #####################

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb"
    Inherits="WebServ.WebForm2"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
    <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:datagrid id="DataGrid1" runat="server" Height="10px" Width="526px"
    AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="EmpName" HeaderText="Name"></asp:BoundColumn>
    <asp:BoundColumn DataField="job" HeaderText="Job"></asp:BoundColumn>
    <asp:BoundColumn DataField="Sal" HeaderText="Salary"></asp:BoundColumn>
    </Columns>
    </asp:datagrid></form>
    </body>
    </HTML>
    #########################################################

    ##################
    File Name:WebForm2.aspx.vb
    #############

    Imports System.Data
    Imports System.Data.SqlClient

    Public Class WebForm2
    Inherits System.Web.UI.Page
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    #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
    Dim Conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    Dim Sqlstr As String

    Sqlstr = "select * from emp"
    Dim myCommand As New SqlCommand(Sqlstr, Conn)

    myCommand.CommandType = CommandType.Text
    Conn.Open()
    Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    DataGrid1.DataSource = result
    DataGrid1.DataBind()

    End Sub

    End Class


  5. #5
    Hema Guest

    Re: How to use Datagrid in ASP.NET?


    I used following code. I got following error at line---
    Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)---
    Any problem with "connectionString" syntax?
    **********************
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Handles MyBase.Load
    Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN=ITS"))
    Dim Sqlstr As String
    Sqlstr = "select * from author"
    Dim myCommand As New SqlCommand(Sqlstr, objConn)
    myCommand.CommandType = CommandType.Text
    Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    DataGrid1.DataSource = result
    DataGrid1.DataBind()
    End Sub
    ***************
    ExecuteReader requires an open and available Connection. The connection's
    current state is Closed.
    ***************



    "Raghu" <reach_raghue@mailfromx.com> wrote:
    >
    >all data types are available in asp.net
    >you have to import name sapaces..
    >Iam sending two files try it.
    >
    >################
    >File Name Webform2.aspx
    >#####################
    >
    ><%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb"
    >Inherits="WebServ.WebForm2"%>
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    ><HTML>
    > <HEAD>
    > <title>WebForm2</title>
    > <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
    > <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
    > <meta content="JavaScript" name="vs_defaultClientScript">
    > <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    > </HEAD>
    > <body>
    > <form id="Form1" method="post" runat="server">
    > <asp:datagrid id="DataGrid1" runat="server" Height="10px" Width="526px"
    >AutoGenerateColumns="False">
    > <Columns>
    > <asp:BoundColumn DataField="EmpName" HeaderText="Name"></asp:BoundColumn>
    > <asp:BoundColumn DataField="job" HeaderText="Job"></asp:BoundColumn>
    > <asp:BoundColumn DataField="Sal" HeaderText="Salary"></asp:BoundColumn>
    > </Columns>
    > </asp:datagrid></form>
    > </body>
    ></HTML>
    >#########################################################
    >
    >##################
    >File Name:WebForm2.aspx.vb
    >#############
    >
    >Imports System.Data
    >Imports System.Data.SqlClient
    >
    >Public Class WebForm2
    > Inherits System.Web.UI.Page
    > Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    >
    >#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
    > Dim Conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    >
    > Dim Sqlstr As String
    >
    > Sqlstr = "select * from emp"
    > Dim myCommand As New SqlCommand(Sqlstr, Conn)
    >
    > myCommand.CommandType = CommandType.Text
    > Conn.Open()
    > Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > DataGrid1.DataSource = result
    > DataGrid1.DataBind()
    >
    > End Sub
    >
    >End Class
    >



  6. #6
    Hema Guest

    Re: How to use Datagrid in ASP.NET?


    I am sorry. I have MS Access database. I can not use SQLDataReader.
    Can you give me syntax for MS Access?

    "Hema" <hemakumar65@hotmail.com> wrote:
    >
    >I used following code. I got following error at line---
    >Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)---
    >Any problem with "connectionString" syntax?
    >**********************
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    >Handles MyBase.Load
    > Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN=ITS"))
    > Dim Sqlstr As String
    > Sqlstr = "select * from author"
    > Dim myCommand As New SqlCommand(Sqlstr, objConn)
    > myCommand.CommandType = CommandType.Text
    > Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > DataGrid1.DataSource = result
    > DataGrid1.DataBind()
    > End Sub
    >***************
    >ExecuteReader requires an open and available Connection. The connection's
    >current state is Closed.
    >***************
    >
    >
    >
    >"Raghu" <reach_raghue@mailfromx.com> wrote:
    >>
    >>all data types are available in asp.net
    >>you have to import name sapaces..
    >>Iam sending two files try it.
    >>
    >>################
    >>File Name Webform2.aspx
    >>#####################
    >>
    >><%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb"
    >>Inherits="WebServ.WebForm2"%>
    >><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    >><HTML>
    >> <HEAD>
    >> <title>WebForm2</title>
    >> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
    >> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
    >> <meta content="JavaScript" name="vs_defaultClientScript">
    >> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    >> </HEAD>
    >> <body>
    >> <form id="Form1" method="post" runat="server">
    >> <asp:datagrid id="DataGrid1" runat="server" Height="10px" Width="526px"
    >>AutoGenerateColumns="False">
    >> <Columns>
    >> <asp:BoundColumn DataField="EmpName" HeaderText="Name"></asp:BoundColumn>
    >> <asp:BoundColumn DataField="job" HeaderText="Job"></asp:BoundColumn>
    >> <asp:BoundColumn DataField="Sal" HeaderText="Salary"></asp:BoundColumn>
    >> </Columns>
    >> </asp:datagrid></form>
    >> </body>
    >></HTML>
    >>#########################################################
    >>
    >>##################
    >>File Name:WebForm2.aspx.vb
    >>#############
    >>
    >>Imports System.Data
    >>Imports System.Data.SqlClient
    >>
    >>Public Class WebForm2
    >> Inherits System.Web.UI.Page
    >> Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    >>
    >>#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
    >> Dim Conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    >>
    >> Dim Sqlstr As String
    >>
    >> Sqlstr = "select * from emp"
    >> Dim myCommand As New SqlCommand(Sqlstr, Conn)
    >>
    >> myCommand.CommandType = CommandType.Text
    >> Conn.Open()
    >> Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    >> DataGrid1.DataSource = result
    >> DataGrid1.DataBind()
    >>
    >> End Sub
    >>
    >>End Class
    >>

    >



  7. #7
    Jason Guest

    Re: How to use Datagrid in ASP.NET?


    "Hema" <hemakumar65@hotmail.com> wrote:
    >
    >Data should be pulled from database and displayed in datagrid. I prefer

    to
    >write code in aspx.vb instead of aspx. Can you please provide me code.
    >

    If you are using VS.NET you can go to help and type walkthrough:datagrid
    and there is a very detailed description of how to set up datagrids. I assume
    you mean codebehind files when you say "aspx.vb" which is the correct way
    to seperate code from content(applause). If your not using
    VS.NET you just need to remember that in the .vb file you have to declare
    your datagrid like this
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    or you will get an error about datagrid1 no being declared. Otherwise all
    the other stuff (i.e. oleDbDataAdapter, dataset, etc. are the same)
    js

  8. #8
    Join Date
    Aug 2008
    Posts
    3

    MyReplay

    It's Depend on you.
    What would you like to do.
    Datagride is not a small control.
    Its uses to perform multiple functionality.
    Just says perticularly What to do ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center