Help with Datagrid (Surprising No Data!!)

Discussion in 'ASP .Net' started by naija naija, Sep 30, 2004.

  1. naija naija

    naija naija Guest

    Hello guys i made a Datagrid with Editing,Update and Cancel using
    VS.NET. to my surprise nothing is on the screen after compilation ..

    By code below:-
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.HtmlControls
    Public Class datagrid_edit
    Inherits System.Web.UI.Page

    Protected WithEvents MenuGrid 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

    'NOTE: The following placeholder declaration is required by the Web
    Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    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
    'Put user code to initialize the page here
    End Sub
    Sub BindDataGrid()

    Dim DBConnection As SqlConnection
    Dim DBCommand As SqlCommand
    Dim DBReader As SqlDataReader
    Dim SQLString As String

    DBConnection = New
    SqlConnection("server=(local);database=Northwind;integrated
    security=true;")
    DBConnection.Open()
    SQLString = "SELECT * FROM treetable ORDER BY FileCategoryID"
    DBCommand = New SqlCommand(SQLString, DBConnection)
    DBReader = DBCommand.ExecuteReader()
    MenuGrid.DataSource = DBReader
    MenuGrid.DataBind()
    DBReader.Close()
    DBConnection.Close()

    End Sub

    Sub EditRecord(ByVal Src As Object, ByVal Args As
    DataGridCommandEventArgs)
    MenuGrid.EditItemIndex = Args.Item.ItemIndex
    BindDataGrid()
    End Sub

    Sub UpdateRecord(ByVal Src As Object, ByVal Args As
    DataGridCommandEventArgs)
    Dim DBConnection As SqlConnection
    Dim DBCommand As SqlCommand
    Dim DBReader As SqlDataReader
    Dim SQLString As String

    Dim FileCategoryID = CType(Args.Item.Cells(0).Controls(0),
    TextBox).Text
    Dim ParentID = CType(Args.Item.Cells(1).Controls(0),
    TextBox).Text
    Dim FileCategory = CType(Args.Item.Cells(2).Controls(0),
    TextBox).Text
    Dim NavigateUrl = CType(Args.Item.Cells(3).Controls(0),
    TextBox).Text
    Dim Target = CType(Args.Item.Cells(4).Controls(0), TextBox).Text

    'Dim ItemPrice = CType(Args.Item.Cells(4).Controls(0),
    TextBox).Text
    'Dim ItemQuantity = CType(Args.Item.Cells(5).Controls(0),
    TextBox).Text

    SQLString = "UPDATE treetable SET " & _
    "ParentID = '" & ParentID & "', " & _
    "FileCategory = '" & Replace(FileCategory, "'", "''") & "', "
    & _
    "NacigateUrl = '" & Replace(NavigateUrl, "'", "''") & "', " &
    _
    "Target = " & Target & ", " & _
    "WHERE FileCategoryID ='" & FileCategoryID & "'"
    '"ItemQuantity = " & ItemQuantity & " " & _
    '"WHERE ItemNumber = '" & ItemNumber & "'"

    DBConnection = New
    SqlConnection("server=(local);database=Northwind;integrated
    security=true;")
    DBConnection.Open()
    DBCommand = New SqlCommand(SQLString, DBConnection)
    DBCommand.ExecuteNonQuery()
    DBConnection.Close()

    MenuGrid.EditItemIndex = -1
    BindDataGrid()

    End Sub


    Sub CancelRecord(ByVal Src As Object, ByVal Args As
    DataGridCommandEventArgs)
    MenuGrid.EditItemIndex = -1
    BindDataGrid()
    End Sub

    End Class

    Html Code Below:-
    -------------------

    <asp:DataGrid id="MenuGrid" runat="server"
    OnEditCommand="EditRecord"
    OnUpdateCommand="UpdateRecord"
    OnCancelCommand="CancelRecord"
    AutoGenerateColumns="False">

    <Columns>

    <asp:BoundColumn
    HeaderText="ParentID"
    DataField="ParentID"/>
    <asp:BoundColumn
    HeaderText="FileCategory"
    DataField="FileCategory"/>
    <asp:BoundColumn
    HeaderText="NavigateUrl"
    DataField="NavigateUrl"/>
    <asp:BoundColumn
    HeaderText="target"
    DataField="target"/>

    <asp:EditCommandColumn
    HeaderText="Editing"
    EditText="Edit"
    UpdateText="Update"
    CancelText="Cancel"/>

    </Columns>

    </asp:DataGrid>




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    naija naija, Sep 30, 2004
    #1
    1. Advertising

  2. naija naija

    Hermit Dave Guest

    Well the reason you cant see any data is that you are not requesting for
    any. I have added a couple of lines to Page_load

    Also, using consider using disconnected objects like dataset for what you
    are doing.
    ie fetch a DataSet. use DataAdapter.Fill() for that purpose. that way you
    can close the connection after the fill command is complete.
    And you should also try using
    Try Catch blocks..

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    ' for first time population of datagrid

    if not Page.IsPostback then
    BindDataGrid()
    end if
    End Sub

    Sub BindDataGrid()

    Dim DBConnection As SqlConnection
    Dim DBCommand As SqlCommand
    ' the rest of the code
    end sub
    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "naija naija" <> wrote in message
    news:...
    > Hello guys i made a Datagrid with Editing,Update and Cancel using
    > VS.NET. to my surprise nothing is on the screen after compilation ..
    >
    > By code below:-
    > Imports System.Data
    > Imports System.Data.SqlClient
    > Imports System.Web.UI.WebControls
    > Imports System.Web.UI.HtmlControls
    > Public Class datagrid_edit
    > Inherits System.Web.UI.Page
    >
    > Protected WithEvents MenuGrid 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
    >
    > 'NOTE: The following placeholder declaration is required by the Web
    > Form Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > 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
    > 'Put user code to initialize the page here
    > End Sub
    > Sub BindDataGrid()
    >
    > Dim DBConnection As SqlConnection
    > Dim DBCommand As SqlCommand
    > Dim DBReader As SqlDataReader
    > Dim SQLString As String
    >
    > DBConnection = New
    > SqlConnection("server=(local);database=Northwind;integrated
    > security=true;")
    > DBConnection.Open()
    > SQLString = "SELECT * FROM treetable ORDER BY FileCategoryID"
    > DBCommand = New SqlCommand(SQLString, DBConnection)
    > DBReader = DBCommand.ExecuteReader()
    > MenuGrid.DataSource = DBReader
    > MenuGrid.DataBind()
    > DBReader.Close()
    > DBConnection.Close()
    >
    > End Sub
    >
    > Sub EditRecord(ByVal Src As Object, ByVal Args As
    > DataGridCommandEventArgs)
    > MenuGrid.EditItemIndex = Args.Item.ItemIndex
    > BindDataGrid()
    > End Sub
    >
    > Sub UpdateRecord(ByVal Src As Object, ByVal Args As
    > DataGridCommandEventArgs)
    > Dim DBConnection As SqlConnection
    > Dim DBCommand As SqlCommand
    > Dim DBReader As SqlDataReader
    > Dim SQLString As String
    >
    > Dim FileCategoryID = CType(Args.Item.Cells(0).Controls(0),
    > TextBox).Text
    > Dim ParentID = CType(Args.Item.Cells(1).Controls(0),
    > TextBox).Text
    > Dim FileCategory = CType(Args.Item.Cells(2).Controls(0),
    > TextBox).Text
    > Dim NavigateUrl = CType(Args.Item.Cells(3).Controls(0),
    > TextBox).Text
    > Dim Target = CType(Args.Item.Cells(4).Controls(0), TextBox).Text
    >
    > 'Dim ItemPrice = CType(Args.Item.Cells(4).Controls(0),
    > TextBox).Text
    > 'Dim ItemQuantity = CType(Args.Item.Cells(5).Controls(0),
    > TextBox).Text
    >
    > SQLString = "UPDATE treetable SET " & _
    > "ParentID = '" & ParentID & "', " & _
    > "FileCategory = '" & Replace(FileCategory, "'", "''") & "', "
    > & _
    > "NacigateUrl = '" & Replace(NavigateUrl, "'", "''") & "', " &
    > _
    > "Target = " & Target & ", " & _
    > "WHERE FileCategoryID ='" & FileCategoryID & "'"
    > '"ItemQuantity = " & ItemQuantity & " " & _
    > '"WHERE ItemNumber = '" & ItemNumber & "'"
    >
    > DBConnection = New
    > SqlConnection("server=(local);database=Northwind;integrated
    > security=true;")
    > DBConnection.Open()
    > DBCommand = New SqlCommand(SQLString, DBConnection)
    > DBCommand.ExecuteNonQuery()
    > DBConnection.Close()
    >
    > MenuGrid.EditItemIndex = -1
    > BindDataGrid()
    >
    > End Sub
    >
    >
    > Sub CancelRecord(ByVal Src As Object, ByVal Args As
    > DataGridCommandEventArgs)
    > MenuGrid.EditItemIndex = -1
    > BindDataGrid()
    > End Sub
    >
    > End Class
    >
    > Html Code Below:-
    > -------------------
    >
    > <asp:DataGrid id="MenuGrid" runat="server"
    > OnEditCommand="EditRecord"
    > OnUpdateCommand="UpdateRecord"
    > OnCancelCommand="CancelRecord"
    > AutoGenerateColumns="False">
    >
    > <Columns>
    >
    > <asp:BoundColumn
    > HeaderText="ParentID"
    > DataField="ParentID"/>
    > <asp:BoundColumn
    > HeaderText="FileCategory"
    > DataField="FileCategory"/>
    > <asp:BoundColumn
    > HeaderText="NavigateUrl"
    > DataField="NavigateUrl"/>
    > <asp:BoundColumn
    > HeaderText="target"
    > DataField="target"/>
    >
    > <asp:EditCommandColumn
    > HeaderText="Editing"
    > EditText="Edit"
    > UpdateText="Update"
    > CancelText="Cancel"/>
    >
    > </Columns>
    >
    > </asp:DataGrid>
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Hermit Dave, Sep 30, 2004
    #2
    1. Advertising

  3. Hi Hermit,
    Thanks for the reply.
    I got that doing already guess i was tired when i wrote that code!
    i will definitely amke use of dataset later!




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    naijacoder naijacoder, Oct 2, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. janek
    Replies:
    3
    Views:
    431
    Ioannis Vranos
    Feb 21, 2005
  2. Michael Olea

    A Surprising Partial Specialization

    Michael Olea, Jun 14, 2005, in forum: C++
    Replies:
    1
    Views:
    323
    Pete Becker
    Jun 14, 2005
  3. Michael Cornelius

    Integer division, surprising results

    Michael Cornelius, May 10, 2004, in forum: Python
    Replies:
    2
    Views:
    370
    Steve
    May 12, 2004
  4. Tim Peters
    Replies:
    1
    Views:
    372
    Ralf Muschall
    May 12, 2004
  5. lrotger
    Replies:
    0
    Views:
    472
    lrotger
    Jul 3, 2006
Loading...

Share This Page