Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I need to edit a datagrid. I am using Visual Studio.NET for my development tool.

 

I have an .aspx, .aspx.vb and a .vb page.

My .aspx page has the data grid with: OnEditCommand="dgrdDivision_EditCommand"

OnUpdateCommand="dgrdDivision_UpdateCommand"

OnCancelCommand="dgrdDivision_CancelCommand"

and an EditCommandColumn corresponding to the edit, update and cancel features.

 

In my class file I have a function:

 Public Function InsertIntoDivisionDb(ByVal dgrdDivision As DataGrid) As clDivision
       BindDataGrid()
       dgrdDivision_EditCommand()
       dgrdDivision_UpdateCommand()
   End Function

I am calling the sub routines which hold the connection string,

edit code:dgrdDivision.EditItemIndex = e.Item.ItemIndex.

And the third sub routine has the update code:

   Sub dgrdDivision_UpdateCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
       Dim txtDivisionCode As TextBox
       Dim txtDivisionName As TextBox
       Dim strDivisionCode As String
       Dim strDivisionName As String
       Dim conn As SqlConnection = openDatabase()

       txtDivisionCode = dgrdDivision.DataKeys(e.Item.ItemIndex)
       txtDivisionCode = e.Item.Cells(0).Controls(0)
       txtDivisionName = e.Item.Cells(1).Controls(0)
       strDivisionCode = txtDivisionCode.Text
       strDivisionName = txtDivisionName.Text
       strSQL = "Update DivisionCode Set DivisionCode=@DivCode, " _
        & "DivisionName=@TrackingDivisionName Where DivisionCode=@DivisionCode"
       cmdSQL = New SqlCommand(strSQL, conn)
       cmdSQL.Parameters.Add("@DivisionCode", strDivisionCode)
       cmdSQL.Parameters.Add("@DivisionName", strDivisionName)

       openDatabase()
       cmdSQL.ExecuteNonQuery()
       closeDatabase(conn)
       BindDataGrid()

       dgrdDivision.EditItemIndex = -1

   End Sub

The error reads:

BC30456: 'dgrdDivision_EditCommand' is not a member of 'ASP.UpdateDivisionInfo_aspx'.

 

I don't understant the error as I have the sub routine dgrdDivision_EditCommand.

 

What am I a not seeing?

 

Thank you.

Edited by Robby
  • Moderators
Posted

You don't have a sub called 'dgrdDivision_EditCommand'

 

Where are you calling 'InsertIntoDivisionDb()' from ? And why are you calling the Edit and Update from it?

 

The Update, Edit, Delete are supposed to be called after the user clicks then posts back of there asigned controls.

Visit...Bassic Software
Posted

On my .vb page within the class

I have sub titled:

Sub dgrdDivision_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

dgrdDivision.EditItemIndex = e.Item.ItemIndex

BindDataGrid()

End Sub

 

On my .aspx.vb page I have the following:

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

 

Dim DivisionName As New clDivision()

 

If Not IsPostBack Then

DivisionName.InsertIntoDivisionDb(dgrdDivision)

End If

End Sub

  • Moderators
Posted

This function.....

 

Public Function InsertIntoDivisionDb(ByVal dgrdDivision As DataGrid) As clDivision
       BindDataGrid()
       dgrdDivision_EditCommand()
       dgrdDivision_UpdateCommand()
   End Function

 

...makes no sense at all, you're passing in the datagrid but you don't use it and the function is supposed to return a Class but it does not return anything.

 

I suggest just changing the Page load event to the following ....

 

Private Sub Page_Load(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load
   If Not IsPostBack Then
        BindDataGrid()
   End If
End Sub

Visit...Bassic Software
Posted

Sounds like your missing code in the HTML part of your page.

Open your aspx page and then click the HTML tag on the bottom. This will show you the code for your design. For your datagrid you should have something like...

 

<asp:datagrid id="YOURGRIDNAMEHERE" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 1px" runat="server" OnEditCommand="dgrdDivision_EditCommand">

 

Then you need to setup an edit bound column which I'm assuming you did.

 

Then save, click back to Design and double click to access your vb code.

 

Then you'll need something like

 

Sub grdDivision_EditCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

 

What ever you want it to do goes here

 

End sub

Posted

I made the suggested changes and things are now worst. I freely confess that I am not certain as to how to implement the correct solution. As such, I am posting my code for a) my web form, .aspx; and b) the code behind file, .aspx.vb. Would you please let me know what to do?

 

Thank you, so very kindly.

LadyGail

 

WebForm-.ASPX

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="UpdateDivisionInfo.aspx.vb" Inherits="TestGale411.UpdateDivisionInfo"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<title>UpdateDivisionInfo</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 MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

<asp:label id="Label1" style="Z-INDEX: 101; LEFT: 122px; POSITION: absolute; TOP: 57px" runat="server" Font-Bold="True" Font-Size="Large">Update Division Information</asp:label>

<asp:datagrid id="dgrdDivision" style="Z-INDEX: 102; LEFT: 129px; POSITION: absolute; TOP: 103px" runat="server" HeaderStyle-BackColor="Salmon" CellPadding="10" AutoGenerateColumns="False" DataKeyField="DivCode" OnCancelCommand="dgrdDivision_CancelCommand" OnUpdateCommand="dgrdDivision_UpdateCommand" OnEditCommand="dgrdDivision_EditCommand" Height="206px" Width="350px">

<Columns>

<asp:BoundColumn HeaderText="Division Code" DataField="DivCode" />

<asp:BoundColumn HeaderText="Division Name" DataField="TrackingDivisionName" />

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

</Columns>

</asp:datagrid></form>

</body>

</HTML>

**************************************************

CODE BEHIND FILE-.ASPX.VB

Imports System.Data

Imports System.Data.SqlClient

 

Public Class UpdateDivisionInfo

Inherits System.Web.UI.Page

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Protected WithEvents dgrdDivision As System.Web.UI.WebControls.DataGrid

 

Dim DivCode As String

Dim DivName As String

Dim strSQL As String

 

 

#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

'Put user code to initialize the page here

 

Dim DivisionName As New clDivision()

 

If Not IsPostBack Then

DivisionName.BindDataGrid()

End If

End Sub

 

Sub dgrdDivision_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

dgrdDivision.EditItemIndex = e.Item.ItemIndex

 

End Sub

 

Sub dgrdDivision_UpdateCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

Dim txtDivisionCode As TextBox

Dim txtDivisionName As TextBox

Dim strDivisionCode As String

Dim strDivisionName As String

Dim conn As SqlConnection = openDatabase()

 

txtDivisionCode = dgrdDivision.DataKeys(e.Item.ItemIndex)

txtDivisionCode = e.Item.Cells(0).Controls(0)

txtDivisionName = e.Item.Cells(1).Controls(0)

strDivisionCode = txtDivisionCode.Text

strDivisionName = txtDivisionName.Text

strSQL = "Update DivisionCode Set DivisionCode=@DivCode, " _

& "DivisionName=@TrackingDivisionName Where DivisionCode=@DivisionCode"

cmdSQL = New SqlCommand(strSQL, conn)

cmdSQL.Parameters.Add("@DivisionCode", strDivisionCode)

cmdSQL.Parameters.Add("@DivisionName", strDivisionName)

 

openDatabase()

cmdSQL.ExecuteNonQuery()

closeDatabase(conn)

BindDataGrid()

 

dgrdDivision.EditItemIndex = -1

 

End Sub

 

Public Function openDatabase()

Dim conn As New SqlConnection _

("data source=S0960028;initial catalog=POWER;user id=gu;password=wasdall")

Return conn

 

End Function

 

Private Function closeDatabase(ByVal conn)

conn.close()

conn = Nothing

End Function

 

End Class

  • Administrators
Posted

try changing the update and command handlers so they include the handles keyword (see below). Couldn't test the code though as the bit you posted doesn't contain a variable called cmdSQL or a function called BindDataGrid.

 

Sub dgrdDivision_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles dgrdDivision.EditCommand
       dgrdDivision.EditItemIndex = e.Item.ItemIndex

   End Sub

   Sub dgrdDivision_UpdateCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles dgrdDivision.UpdateCommand
       Dim txtDivisionCode As TextBox
       Dim txtDivisionName As TextBox
       Dim strDivisionCode As String
       Dim strDivisionName As String
       Dim conn As SqlConnection = openDatabase()

       txtDivisionCode = dgrdDivision.DataKeys(e.Item.ItemIndex)
       txtDivisionCode = e.Item.Cells(0).Controls(0)
       txtDivisionName = e.Item.Cells(1).Controls(0)
       strDivisionCode = txtDivisionCode.Text
       strDivisionName = txtDivisionName.Text
       strSQL = "Update DivisionCode Set DivisionCode=@DivCode, " _
       & "DivisionName=@TrackingDivisionName Where DivisionCode=@DivisionCode"
       cmdSQL = New SqlCommand(strSQL, conn)
       cmdSQL.Parameters.Add("@DivisionCode", strDivisionCode)
       cmdSQL.Parameters.Add("@DivisionName", strDivisionName)

       openDatabase()
       cmdSQL.ExecuteNonQuery()
       closeDatabase(conn)
       BindDataGrid()

       dgrdDivision.EditItemIndex = -1

   End Sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

My error for not including all of the file contents.

This has the BindDataGrid().

I made the changes you suggested and now am getting the following error:

Compiler Error Message: BC30456: 'dgrdDivision_EditCommand' is not a member of 'ASP.UpdateDivisionInfo_aspx'.

 

 

 

Imports System.Data

Imports System.Data.SqlClient

 

Public Class UpdateDivisionInfo

Inherits System.Web.UI.Page

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Protected WithEvents dgrdDivision As System.Web.UI.WebControls.DataGrid

 

Dim DivCode As String

Dim DivName As String

Dim strSQL As String

Dim cmdSQL As SqlCommand

 

#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

'Put user code to initialize the page here

 

Dim DivisionName As New clDivision()

 

If Not IsPostBack Then

DivisionName.BindDataGrid()

End If

End Sub

 

Sub BindDataGrid()

Dim conn As SqlConnection = openDatabase()

cmdSQL = New SqlCommand("Select * from DivisionCode", conn)

conn.Open()

dgrdDivision.DataSource = cmdSQL.ExecuteReader()

dgrdDivision.DataBind()

conn.Close()

End Sub

 

Sub dgrdDivision_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles dgrdDivision.EditCommand

dgrdDivision.EditItemIndex = e.Item.ItemIndex

 

End Sub

 

Sub dgrdDivision_UpdateCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles dgrdDivision.UpdateCommand

 

Dim txtDivisionCode As TextBox

Dim txtDivisionName As TextBox

Dim strDivisionCode As String

Dim strDivisionName As String

Dim conn As SqlConnection = openDatabase()

 

txtDivisionCode = dgrdDivision.DataKeys(e.Item.ItemIndex)

txtDivisionCode = e.Item.Cells(0).Controls(0)

txtDivisionName = e.Item.Cells(1).Controls(0)

strDivisionCode = txtDivisionCode.Text

strDivisionName = txtDivisionName.Text

strSQL = "Update DivisionCode Set DivisionCode=@DivCode, " _

& "DivisionName=@TrackingDivisionName Where DivisionCode=@DivisionCode"

cmdSQL = New SqlCommand(strSQL, conn)

cmdSQL.Parameters.Add("@DivisionCode", strDivisionCode)

cmdSQL.Parameters.Add("@DivisionName", strDivisionName)

 

openDatabase()

cmdSQL.ExecuteNonQuery()

closeDatabase(conn)

BindDataGrid()

 

dgrdDivision.EditItemIndex = -1

 

End Sub

 

Public Function openDatabase()

Dim conn As New SqlConnection _

("data source=S0960028;initial catalog=POWER;user id=gu;password=wasdall")

Return conn

 

End Function

 

Private Function closeDatabase(ByVal conn)

conn.close()

conn = Nothing

End Function

 

End Class

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...