Datagrid and update command

This is a discussion on "Datagrid and update command" within the ASP.NET Forum section. This forum, and the thread "Datagrid and update command are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > ASP.NET Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old May 16th, 2006, 05:06
New Member
Join Date: May 2006
Location: UK
Age: 24
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Datagrid and update command

Hi I need your help with this please :brrr:
I am retreiving items from Access Database to a datagrid
and this works fine :tntworth: ... I have and edit options where I want the user to be able to edit the status only!
(MS ACCESS and ASP.NET)
I did the coding but it doesnt work ..
PHP: Select all

Sub DataGrid1_Update(Source As ObjectAs DataGridCommandEventArgs)
 
             
Dim connectionString As String "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Inetpub\wwwr"_
                
"oot\HASEM_sys\HASEM_sys.mdb"
             
Dim myConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

            
Dim Status As TextBox
            Status 
Ctype(E.Item.Cells(7).Controls(0), TextBox)
            
Dim ItemNo As TextBox
            ItemNo 
Me.DataGrid1.DataKeys(E.Item.ItemIndex)
            
Dim strUpdateStmt As String

            strUpdateStmt 
"UPDATE tblItems SET Status = @Status WHERE ItemNo = @ItemNo"
            
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand
.CommandText strUpdateStmt
            dbCommand
.Connection myConnection
            Dim dbParam_status 
As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_status
.ParameterName "@Status"
            'Call the Text property of the Status field as it is a TextBox control.
            dbParam_status.Value = Status.Text
            dbParam_status.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_status)
            Dim dbParam_itemNo As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_itemNo.ParameterName = "@ItemNo"
            dbParam_itemNo.Value = Me.DataGrid1.DataKeys(E.Item.ItemIndex)
            dbParam_itemNo.DbType = System.Data.DbType.Int32
            dbCommand.Parameters.Add(dbParam_itemNo)

            '
Now execute the query against the database
            dbCommand
.ExecuteNonQuery
            
'You need to create a datasource the same way you did when you originally bound the control.
            connectionString  = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Inetpub\wwwroot\HASEM_sys\HASEM_sys.mdb"
            Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
            Dim queryString = "SELECT ItemNo, ItemName, SenderName, SerialNo, ReceivedDt, Model, Quantity, Status From tblItems where Year(ReceivedDt)=Year(Now) And Month(ReceivedDt)=Month(Now) And Day(ReceivedDt)=Day(Now)"
            dbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection
             Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
             dataAdapter.SelectCommand = dbCommand
             Dim dataSet As System.Data.DataSet = New System.Data.DataSet
             dataAdapter.Fill(dataSet)
            '
Create a new view.
            
Dim oView As New DataView(dataSet.Tables(0))
            
'Set up the data grid and bind the data.
            DataGrid1.DataSource = oView
            '
Now that your data is updated you need to repopulate the datagrid and in the process deselect the selected row.
            
DataGrid1.EditItemIndex = -1
            DataGrid1
.DataSource dataSet
 
            Finally bind the source to the control 
and you should see you changes.
            
DataGrid1.DataBind()
    
End Sub 
ERROR:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Source Error:

Line 63:
Line 64: Dim ItemNo As TextBox
Line 65: ItemNo = Me.DataGrid1.DataKeys(E.Item.ItemIndex)
Line 66:
Line 67: Dim strUpdateStmt As String

:chomp: Help please
Reply With Quote

Reply

Tags
datagrid, update, command

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Asp to execute a command in a .exe file Andrew1986 Classic ASP 7 Jan 11th, 2008 09:44
problem running php from the command line edwarrj1 PHP Forum 2 Dec 5th, 2006 18:06
Need help with document.write command MojoP JavaScript Forum 0 Aug 15th, 2005 18:15
VB: Datagrid Update Query - Data Dissapering DJ1UK ASP.NET Forum 2 Aug 8th, 2005 12:36
VS.NET, TextBox, overlay, DataGrid Smokie ASP.NET Forum 10 Jul 27th, 2004 12:17


All times are GMT. The time now is 00:16.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43