VB.NET -Web App: Problems Updating Datagrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tanya2001
    New Member
    • Nov 2007
    • 23

    VB.NET -Web App: Problems Updating Datagrid

    hi all..
    I am trying to update my datagrid in my webform...but its not getting updated..though in the database it removing the <null> and inserting a blank field....its not taking the input which m typing...can sombody plz help me out in this..i need help urgently
    I am posting the code in vb.net for the webform as well as html
    [code=vbnet]
    Protected Sub DataGrid1_Updat eCommand(ByVal source As Object, ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles DataGrid1.Updat eCommand
    Dim Stocksent1 As TextBox = CType(e.Item.Fi ndControl("Text Box2"), TextBox)
    Dim Signed1 As TextBox = CType(e.Item.Fi ndControl("Sign edTextBox"), TextBox)
    Dim Items1 As String = DataGrid1.DataK eys(e.Item.Item Index).ToString ()
    Dim myConnection As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
    myConnection.Op en()

    myCommand1 = New SqlCommand("Upd ate Table3 set Stocksent='" & Stocksent1.Text & "', Signedby='" & Signed1.Text & "' , Flag=0, Sentdate=getdat e() where Items='" & Items1 & "' ", myConnection)
    'myCommand1.Par ameters.Add("@I tems", SqlDbType.NVarC har).Value = Items
    'myCommand1.Par ameters.Add("@S tocksent1", SqlDbType.NVarC har).Value = Stocksent1.Text
    'myCommand1.Par ameters.Add("@S ignedby1", SqlDbType.NVarC har).Value = Signed1.Text
    ra = myCommand1.Exec uteNonQuery()
    myConnection.Cl ose()
    Response.Write( "Records Updated")
    DataGrid1.EditI temIndex = -1
    DataGrid1.DataB ind()
    DataGrid1.Colum ns(8).Visible = False
    DataGrid1.Colum ns(9).Visible = False
    DataGrid1.Colum ns(10).Visible = False
    DataGrid1.Colum ns(11).Visible = False
    DataGrid1.Colum ns(12).Visible = False

    End Sub
    [/code]
    HTML CODE
    [code=asp]
    <form id="Form1" method="post" runat="server">
    <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 296px"
    runat="server" AutoGenerateCol umns="False" DataKeyField="I tems" Width="848px" ForeColor="DimG ray"
    BackColor="Whea t" BorderColor="#6 60033" Height="72px">
    <HeaderStyle Font-Bold="True" ForeColor="Choc olate" BackColor="Ligh tGray"></HeaderStyle>
    <Columns>
    <asp:BoundColum n DataField="Logn umm" ReadOnly="True" HeaderText="Log numm"></asp:BoundColumn >
    <asp:BoundColum n DataField="Requ estdate" ReadOnly="True" HeaderText="Req uestdate"></asp:BoundColumn >
    <asp:BoundColum n DataField="Hosp ital" ReadOnly="True" HeaderText="Hos pital"></asp:BoundColumn >
    <asp:BoundColum n DataField="Item s" ReadOnly="True" HeaderText="Ite ms"></asp:BoundColumn >
    <asp:BoundColum n DataField="Curr entstock" ReadOnly="True" HeaderText="Cur rentstock"></asp:BoundColumn >
    <asp:BoundColum n DataField="Stoc krequest" ReadOnly="True" HeaderText="Sto ckrequest"></asp:BoundColumn >
    <asp:BoundColum n DataField="Prio rity" ReadOnly="True" HeaderText="Pri ority"></asp:BoundColumn >
    <asp:BoundColum n DataField="Comm ent" ReadOnly="True" HeaderText="Com ment"></asp:BoundColumn >
    <asp:TemplateCo lumn Visible="False" HeaderText="Sto cksent">
    <ItemTemplate >
    <asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval (Container.Data Item , "Stocksent" ) %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTempla te>
    <asp:TextBox id=TextBox2 runat="server" Text='<%# DataBinder.Eval (Container.Data Item , "Stocksent" ) %>'>
    </asp:TextBox>
    <asp:RequiredFi eldValidator id="RequiredFie ldValidator1" runat="server" ErrorMessage="E nter value" ControlToValida te="TextBox2"> </asp:RequiredFie ldValidator>
    </EditItemTemplat e>
    </asp:TemplateCol umn>
    <asp:TemplateCo lumn Visible="False" HeaderText="Sig nedby">
    <ItemTemplate >
    <asp:Label id=Label1 runat="server" NAME="Label1" Text='<%# DataBinder.Eval (Container, "DataItem.Signe dby") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTempla te>
    <asp:TextBox id=SignedTextBo x runat="server" Text='<%# DataBinder.Eval (Container, "DataItem.Signe dby") %>'>
    </asp:TextBox>
    <asp:RequiredFi eldValidator id="RequiredFie ldValidator2" runat="server" Width="128px" ErrorMessage="E nter Value"
    ControlToValida te="SignedTextB ox"></asp:RequiredFie ldValidator>
    </EditItemTemplat e>
    </asp:TemplateCol umn>
    <asp:TemplateCo lumn Visible="False" HeaderText="Tak enby">
    <ItemTemplate >
    <asp:Label id="Label3" runat="server"> </asp:Label>
    </ItemTemplate>
    <EditItemTempla te>
    <asp:TextBox id="TextBox1" runat="server"> </asp:TextBox>
    <asp:RequiredFi eldValidator id="RequiredFie ldValidator3" runat="server" ErrorMessage="E nter Value" ControlToValida te="TextBox1"> </asp:RequiredFie ldValidator>
    </EditItemTemplat e>
    </asp:TemplateCol umn>
    <asp:TemplateCo lumn Visible="False" HeaderText="Com ments">
    <ItemTemplate >
    <asp:Label runat="server"> </asp:Label>
    </ItemTemplate>
    <EditItemTempla te>
    <asp:TextBox runat="server"> </asp:TextBox>
    </EditItemTemplat e>
    </asp:TemplateCol umn>
    <asp:TemplateCo lumn HeaderText="Edi t">
    <ItemTemplate >
    <asp:LinkButt on runat="server" CausesValidatio n="false" CommandName="Ed it" Text="Edit" ID="Linkbutton1 "></asp:LinkButton>
    </ItemTemplate>
    <EditItemTempla te>
    <asp:LinkButt on runat="server" CommandName="Up date" Text="Update" ID="Linkbutton2 "></asp:LinkButton>
    <asp:LinkButt on runat="server" CausesValidatio n="false" CommandName="Ca ncel" Text="Cancel" ID="Linkbutton3 "></asp:LinkButton>
    </EditItemTemplat e>
    </asp:TemplateCol umn>
    </Columns>
    </asp:datagrid>[/code]
    Last edited by Frinavale; Nov 15 '07, 07:44 PM. Reason: Added [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by tanya2001
    hi all..
    I am trying to update my datagrid in my webform...but its not getting updated..though in the database it removing the <null> and inserting a blank field....its not taking the input which m typing...can sombody plz help me out in this..i need help urgently
    I am posting the code in vb.net for the webform as well as html...
    Hi Tanya,
    In the future could you please post your code within &#91;code] tags.
    (For example you'd type &#91;code=vb net]'...paste code here...&#91; /code] if you wanted to post vb.net code)
    This just makes it easier for everyone.

    Are you by any chance binding your DataGrid to its DataSource during PageLoad?

    Comment

    • tanya2001
      New Member
      • Nov 2007
      • 23

      #3
      Originally posted by Frinavale
      Hi Tanya,
      In the future could you please post your code within [code] tags.
      (For example you'd type [code=vbnet]'...paste code here...[ /code] if you wanted to post vb.net code)
      This just makes it easier for everyone.

      Are you by any chance binding your DataGrid to its DataSource during PageLoad?

      yes i am do u want me to give u the code for page load event as well?

      Comment

      • mzmishra
        Recognized Expert Contributor
        • Aug 2007
        • 390

        #4
        I think this part is missing in your code
        DataGrid.DataSo urce

        Comment

        • tanya2001
          New Member
          • Nov 2007
          • 23

          #5
          Originally posted by mzmishra
          I think this part is missing in your code
          DataGrid.DataSo urce

          where shud this part be i mean Datagrid.Dataso urce????

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by tanya2001
            where shud this part be i mean Datagrid.Dataso urce????
            Could you please post your code for the PageLoad method?

            Comment

            • tanya2001
              New Member
              • Nov 2007
              • 23

              #7
              Originally posted by Frinavale
              Could you please post your code for the PageLoad method?
              [Code=VBNET]

              Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
              Dim cn As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
              Dim da As New SqlDataAdapter( "select * from Table3 where Lognumm= '" & Request.QuerySt ring("ID") & "' and Flag=1", cn)
              Dim dt As New DataTable
              da.Fill(dt)
              da.Dispose()
              cn.Dispose()
              Me.DataGrid1.Da taSource = dt
              Me.DataGrid1.Da taBind()
              End Sub

              [/Code]
              Last edited by Frinavale; Nov 16 '07, 02:38 PM. Reason: Fixed [code] tags

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by tanya2001
                [Code=VBNET]

                Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
                Dim cn As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
                Dim da As New SqlDataAdapter( "select * from Table3 where Lognumm= '" & Request.QuerySt ring("ID") & "' and Flag=1", cn)
                Dim dt As New DataTable
                da.Fill(dt)
                da.Dispose()
                cn.Dispose()
                Me.DataGrid1.Da taSource = dt
                Me.DataGrid1.Da taBind()
                End Sub

                [/Code]
                Try changing your code so that you only set and bind the DataSource to your DataGrid when it is not isPostBack

                Eg
                [code=vbnet]

                Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load


                If IsPostBack = False Then

                Dim cn As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
                Dim da As New SqlDataAdapter( "select * from Table3 where Lognumm= '" & Request.QuerySt ring("ID") & "' and Flag=1", cn)
                Dim dt As New DataTable
                da.Fill(dt)
                da.Dispose()
                cn.Dispose()
                Me.DataGrid1.Da taSource = dt
                Me.DataGrid1.Da taBind()

                End If
                End Sub
                [/code]


                The reason for this is during your page cycle, when you're in the PageLoad, you can set your control's properties Before ASP.NET sets them. If you do this, then any properties that the control gathered from the user is lost....so if your user is editing an empty row and adding new values to it...and then you reset the data source for your DataGrid in PageLoad, you will lose the data that the user supplied while editing.

                (PS. Please take a look at my not on using the [code] tags again. It should be code=vbnet not code : Vb.Net :) Thanks)

                Comment

                • tanya2001
                  New Member
                  • Nov 2007
                  • 23

                  #9
                  lol....hi
                  well i did write it the way u said as shown below but when i click the edit link its just showing me a blank page the datagrid disappears...th e entire code for the page is shown below( i hope i m right this time :-))

                  [code=vbnet]

                  Imports System.Data.Sql Client
                  Public Class trial
                  Inherits System.Web.UI.P age
                  Dim myCommand1 As New SqlCommand
                  Protected WithEvents LinkButton4 As System.Web.UI.W ebControls.Link Button
                  Protected WithEvents LinkButton5 As System.Web.UI.W ebControls.Link Button
                  Dim ra As Integer

                  #Region " Web Form Designer Generated Code "

                  'This call is required by the Web Form Designer.
                  <System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()

                  End Sub
                  Protected WithEvents DataGrid1 As System.Web.UI.W ebControls.Data Grid

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

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

                  #End Region

                  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
                  If IsPostBack = False Then


                  Dim cn As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
                  Dim da As New SqlDataAdapter( "select * from Table3 where Lognumm= '" & Request.QuerySt ring("ID") & "' and Flag=1", cn)
                  Dim dt As New DataTable
                  da.Fill(dt)
                  da.Dispose()
                  cn.Dispose()
                  Me.DataGrid1.Da taSource = dt
                  Me.DataGrid1.Da taBind()
                  end if
                  End Sub

                  Protected Sub DataGrid1_EditC ommand(ByVal source As Object, ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles DataGrid1.EditC ommand
                  DataGrid1.Colum ns(8).Visible = True
                  DataGrid1.Colum ns(9).Visible = True
                  DataGrid1.Colum ns(10).Visible = True
                  DataGrid1.Colum ns(11).Visible = True

                  DataGrid1.EditI temIndex = e.Item.ItemInde x
                  DataGrid1.DataB ind()

                  End Sub
                  Protected Sub DataGrid1_Updat eCommand(ByVal source As Object, ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles DataGrid1.Updat eCommand

                  Dim Stocksent1 As TextBox = CType(e.Item.Fi ndControl("Text Box2"), TextBox)
                  Dim Signed1 As TextBox = CType(e.Item.Fi ndControl("Sign edTextBox"), TextBox)
                  Dim Items1 As String = DataGrid1.DataK eys(e.Item.Item Index).ToString ()
                  Dim myConnection As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
                  myConnection.Op en()

                  myCommand1 = New SqlCommand("Upd ate Table3 set Stocksent='" & Stocksent1.Text & "', Signedby='" & Signed1.Text & "' , Flag=0, Sentdate=getdat e() where Items='" & Items1 & "' ", myConnection)
                  'myCommand1.Par ameters.Add("@I tems", SqlDbType.NVarC har).Value = Items
                  'myCommand1.Par ameters.Add("@S tocksent1", SqlDbType.NVarC har).Value = Stocksent1.Text
                  'myCommand1.Par ameters.Add("@S ignedby1", SqlDbType.NVarC har).Value = Signed1.Text
                  ra = myCommand1.Exec uteNonQuery()
                  myConnection.Cl ose()
                  Response.Write( "Records Updated")
                  DataGrid1.EditI temIndex = -1
                  DataGrid1.DataB ind()
                  DataGrid1.Colum ns(8).Visible = False
                  DataGrid1.Colum ns(9).Visible = False
                  DataGrid1.Colum ns(10).Visible = False
                  DataGrid1.Colum ns(11).Visible = False
                  DataGrid1.Colum ns(12).Visible = False

                  End Sub
                  Private Sub LinkButton5_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles LinkButton5.Cli ck

                  Response.Redire ct("http://localhost/WebApplication3 \Warehouse1.asp x")

                  End Sub
                  Private Sub LinkButton4_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles LinkButton4.Cli ck

                  Response.Redire ct("http://localhost/WebApplication3 \WebForm6.aspx" )

                  End Sub

                  End Class
                  [/code]
                  Last edited by Frinavale; Nov 16 '07, 03:48 PM. Reason: Fixed [code] tags

                  Comment

                  • tanya2001
                    New Member
                    • Nov 2007
                    • 23

                    #10
                    Originally posted by Frinavale
                    Try changing your code so that you only set and bind the DataSource to your DataGrid when it is not isPostBack

                    Eg
                    [code=vbnet]

                    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load


                    If IsPostBack = False Then

                    Dim cn As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
                    Dim da As New SqlDataAdapter( "select * from Table3 where Lognumm= '" & Request.QuerySt ring("ID") & "' and Flag=1", cn)
                    Dim dt As New DataTable
                    da.Fill(dt)
                    da.Dispose()
                    cn.Dispose()
                    Me.DataGrid1.Da taSource = dt
                    Me.DataGrid1.Da taBind()

                    End If
                    End Sub
                    [/code]


                    The reason for this is during your page cycle, when you're in the PageLoad, you can set your control's properties Before ASP.NET sets them. If you do this, then any properties that the control gathered from the user is lost....so if your user is editing an empty row and adding new values to it...and then you reset the data source for your DataGrid in PageLoad, you will lose the data that the user supplied while editing.

                    (PS. Please take a look at my not on using the [code] tags again. It should be code=vbnet not code : Vb.Net :) Thanks)
                    hi i did it but its like showing me a blank page wehn i click edit...y is it so???

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Originally posted by tanya2001
                      hi i did it but its like showing me a blank page wehn i click edit...y is it so???
                      I think it's because you're not setting the source.
                      You need to set the DataSource before binding in the edit.
                      Try storing your DataSource object in Session and resetting your Grid's DataSource to this before DataBind in your Edit function.

                      Get back to me on whether or not it worked.

                      (PS you've Almost got the [code] tags working for you! just don't use vb.net...use vbnet instead! hehe)

                      Comment

                      • tanya2001
                        New Member
                        • Nov 2007
                        • 23

                        #12
                        Originally posted by Frinavale
                        I think it's because you're not setting the source.
                        You need to set the DataSource before binding in the edit.
                        Try storing your DataSource object in Session and resetting your Grid's DataSource to this before DataBind in your Edit function.

                        Get back to me on whether or not it worked.

                        (PS you've Almost got the [code] tags working for you! just don't use vb.net...use vbnet instead! hehe)
                        hiya sorry to bother u

                        but can u plz write down the code for me as m all new 2 .net i dunno much abt this plzzzz...n yippeee i have finally learnt the code tags....(cheers 2 u)

                        Comment

                        • Frinavale
                          Recognized Expert Expert
                          • Oct 2006
                          • 9749

                          #13
                          Originally posted by tanya2001
                          hiya sorry to bother u

                          but can u plz write down the code for me as m all new 2 .net i dunno much abt this plzzzz...n yippeee i have finally learnt the code tags....(cheers 2 u)
                          Normally we don't write code for you. The experts/moderators/administrators are here to help you understand the problem so that you can solve it.

                          But I'm going to show you what I mean through code anyway...
                          [code=vbnet]
                          Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load


                          If IsPostBack = False Then

                          Dim cn As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
                          Dim da As New SqlDataAdapter( "select * from Table3 where Lognumm= '" & Request.QuerySt ring("ID") & "' and Flag=1", cn)
                          Dim dt As New DataTable
                          da.Fill(dt)
                          da.Dispose()
                          cn.Dispose()
                          Session("dt") = dt 'I am storing the data source in Session so that we can use it again upon postback.
                          Me.DataGrid1.Da taSource = dt
                          Me.DataGrid1.Da taBind()

                          End If
                          End Sub
                          '.............. ............... ...........
                          Protected Sub DataGrid1_EditC ommand(ByVal source As Object, ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles DataGrid1.EditC ommand
                          DataGrid1.Colum ns(8).Visible = True
                          DataGrid1.Colum ns(9).Visible = True
                          DataGrid1.Colum ns(10).Visible = True
                          DataGrid1.Colum ns(11).Visible = True

                          DataGrid1.EditI temIndex = e.Item.ItemInde x
                          DataGrid1.DataS ource = Ctype( Session("dt"), DataTable) 'Here I'm grabbing the data source from session so that we actually bind Data to the data grid...instead of nothing
                          DataGrid1.DataB ind()

                          End Sub[/code]

                          For more information on Sessions please see the .NET Article on Sessions

                          Comment

                          • tanya2001
                            New Member
                            • Nov 2007
                            • 23

                            #14
                            Originally posted by Frinavale
                            Normally we don't write code for you. The experts/moderators/administrators are here to help you understand the problem so that you can solve it.

                            But I'm going to show you what I mean through code anyway...
                            [code=vbnet]
                            Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load


                            If IsPostBack = False Then

                            Dim cn As New SqlConnection(" Server=SERVER;u id=sa;pwd=;Data base=Central")
                            Dim da As New SqlDataAdapter( "select * from Table3 where Lognumm= '" & Request.QuerySt ring("ID") & "' and Flag=1", cn)
                            Dim dt As New DataTable
                            da.Fill(dt)
                            da.Dispose()
                            cn.Dispose()
                            Session("dt") = dt 'I am storing the data source in Session so that we can use it again upon postback.
                            Me.DataGrid1.Da taSource = dt
                            Me.DataGrid1.Da taBind()

                            End If
                            End Sub
                            '.............. ............... ...........
                            Protected Sub DataGrid1_EditC ommand(ByVal source As Object, ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles DataGrid1.EditC ommand
                            DataGrid1.Colum ns(8).Visible = True
                            DataGrid1.Colum ns(9).Visible = True
                            DataGrid1.Colum ns(10).Visible = True
                            DataGrid1.Colum ns(11).Visible = True

                            DataGrid1.EditI temIndex = e.Item.ItemInde x
                            DataGrid1.DataS ource = Ctype( Session("dt"), DataTable) 'Here I'm grabbing the data source from session so that we actually bind Data to the data grid...instead of nothing
                            DataGrid1.DataB ind()

                            End Sub[/code]

                            For more information on Sessions please see the .NET Article on Sessions
                            yes i got it thanx a heap....thanks alot i was rely worried abt this...
                            can i ask u another qeury on my project plzz

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              Originally posted by tanya2001
                              yes i got it thanx a heap....thanks alot i was rely worried abt this...
                              can i ask u another qeury on my project plzz
                              I'm glad to have helped.
                              Please post a new thread in the .NET Forum about your other question :)
                              (unless, of course, it's related to this one)

                              -Frinny

                              Comment

                              Working...