Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guys, Got a problem here, as you see I want to update my database via gridview. my problem is when i hit Edit button, my page went white (looks like empty). I dont know where the problem is . Plss help me
here is my code
Design Source
<pre lang="xml"><%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>

    <style type="text/css">
        /* A scrolable div */
        .GridViewContainer
        {
            overflow: auto;
        }
        /* to freeze column cells and its respecitve header*/
        .FrozenCell
        {
            background-color:yellow;
            position: relative;
            cursor: default;
            left: expression(document.getElementById("GridViewContainer").scrollLeft-2);
        }
        /* for freezing column header*/
        .FrozenHeader
        {
         background-color:yellow;
            position: relative;
            cursor: default;
            top: expression(document.getElementById("GridViewContainer").scrollTop-2);
            z-index: 10;
        }
        /*for the locked columns header to stay on top*/
        .FrozenHeader.locked
        {
            z-index: 99;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="GridViewContainer" class="GridViewContainer" style="width:789px;height:345px;" >
        <asp:GridView ID="GridView1" CssClass="GridView"  runat="server"
         AutoGenerateColumns="False"
         OnRowEditing="GridView1_RowEditing"
         OnRowUpdating="GridView1_RowUpdating"
         OnRowCancelingEdit = "GridView1_RowCancelingEdit"

         Height="338px" Width="784px"  >
            <HeaderStyle CssClass="FrozenHeader" />
            <Columns>
        <asp:CommandField ButtonType="Button" ShowEditButton="True"/>
                   <asp:BoundField DataField="CourseTitle" HeaderText="CourseTitle" >
                       <HeaderStyle CssClass="FrozenCell" />
                       <ItemStyle CssClass="FrozenCell" />
                   </asp:BoundField>
      <asp:TemplateField HeaderText="ID">
          <ItemTemplate>
            <%#Eval("ID")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtID" Text='<%# Eval("ID")%>' />
          </EditItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderText="CourseCode">
          <ItemTemplate>
            <%#Eval("CourseCode")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtCourseCode" Text='<%# Eval("CourseCode")%>' />
          </EditItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderText="Objectives">
          <ItemTemplate>
            <%#Eval("Objectives")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtObjectives" Text='<%# Eval("Objectives")%>' />
          </EditItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderText="Duration">
          <ItemTemplate>
            <%#Eval("Duration")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtDuration" Text='<%# Eval("Duration")%>' />
          </EditItemTemplate>
      </asp:TemplateField>

            </Columns>

        </asp:GridView>
    </div>
    </form>
</body>
</html>


My aspx.vb
<pre lang="vb">Imports System.Web
Imports System
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (IsPostBack = False) Then
            Dim strSQL As String
            Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
            strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan]"
            connection.Open()
            Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
            'myCommand.Parameters.AddWithValue("@CostCenter", drpcc.Text)

            GridView1.DataSource = myCommand.ExecuteReader()
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub Gridview1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
        Dim txtID As TextBox = row.FindControl("txtID")
        'Dim txtCourseTitle As TextBox = row.FindControl("txtCourseTitle")
        Dim txtCourseCode As TextBox = row.FindControl("txtCourseCode")
        Dim txtObjectives As TextBox = row.FindControl("txtObjectives")
        Dim txtDuration As TextBox = row.FindControl("txtDuration")

    End Sub
    Protected Sub Gridview1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        GridView1.DataBind()
    End Sub
    Protected Sub Gridview1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        GridView1.DataBind()
    End Sub

End Class
Posted
Updated 10-May-11 20:13pm
v2

place all code in page_load event in one function and call it in gridview_editing event instead of gridview1.databind() and also in gridview_rowcancelingedit
 
Share this answer
 
Comments
janwel 11-May-11 2:28am    
ill try sir
janwel 11-May-11 2:31am    
thank sir
ive created a public sub and declare it all there
sir here is my code
LakshmiNarayana Nalluri 11-May-11 3:11am    
calll in page load
if(!ispostback)
{
bindgrid();
}
and then in rowupdating event after your code put gridview1.editindex=-1,bindgrid();
<pre lang="vb">Protected Sub Gridview1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
    Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
    Dim txtID As TextBox = row.FindControl("txtID")
    'Dim txtCourseTitle As TextBox = row.FindControl("txtCourseTitle")
    Dim txtCourseCode As TextBox = row.FindControl("txtCourseCode")
    Dim txtObjectives As TextBox = row.FindControl("txtObjectives")
    Dim txtDuration As TextBox = row.FindControl("txtDuration")

End Sub
Protected Sub Gridview1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
    GridView1.EditIndex = e.NewEditIndex
    BindGrid()
End Sub
Protected Sub Gridview1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
    GridView1.EditIndex = -1
    BindGrid()
End Sub

Public Sub BindGrid()
    Dim strSQL As String
    Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
    strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan]"
    connection.Open()
    Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)

    GridView1.DataSource = myCommand.ExecuteReader()
    GridView1.DataBind()
    connection.Close()
End Sub
 
Share this answer
 
place all code in page_load event in one function and call it in gridview_editing event instead of gridview1.databind() and also in gridview_rowcancelingedit



Try!
Happy Coding! ^_^
 
Share this answer
 

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