Partial Class layout2
Inherits System.Web.UI.Page
Dim objConn As Data.OleDb.OleDbConnection
Dim objCmd As Data.OleDb.OleDbCommand
Public WithEvents backgroundWorker1 As System.ComponentModel.BackgroundWorker
Dim strSQL As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("login.mdb") & ";"
objConn = New Data.OleDb.OleDbConnection(strConnString)
objConn.Open()
If Not Page.IsPostBack() Then
BindData()
End If
End Sub
Sub BindData()
strSQL = "SELECT * FROM Product"
Dim dtReader As Data.OleDb.OleDbDataReader
objCmd = New Data.OleDb.OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to GridView ***'
'GridView1.DataSource = dtReader
GridView1.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Function DataTableProduct() As Data.DataTable
Dim strConnString As String
Dim dtAdapter As Data.OleDb.OleDbDataAdapter
Dim dt As New Data.DataTable
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("login.mdb") & ";"
objConn = New Data.OleDb.OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM Product"
dtAdapter = New Data.OleDb.OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
Return dt
End Function
Sub modEditCommand(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.ShowFooter = False
BindData()
End Sub
Sub modCancelCommand(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = -1
GridView1.ShowFooter = True
BindData()
End Sub
Sub modDeleteCommand(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
strSQL = "DELETE FROM Product WHERE ProID = '" & GridView1.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New Data.OleDb.OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
GridView1.EditIndex = -1
BindData()
End Sub
Sub GridView1_RowCommand(ByVal source As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Add" Then
'*** ProID ***'
Dim txtProID As TextBox = CType(GridView1.FooterRow.FindControl("txtAddProID"), TextBox)
'*** ProductName ***'
Dim txtProductName As TextBox = CType(GridView1.FooterRow.FindControl("txtProductName"), TextBox)
'*** Price ***'
Dim txtPrice As TextBox = CType(GridView1.FooterRow.FindControl("txtPrice"), TextBox)
'*** Imaget ***'
Dim txtImage As TextBox = CType(GridView1.FooterRow.FindControl("txtImage"), TextBox)
'*** Details ***'
Dim txtDetails As TextBox = CType(GridView1.FooterRow.FindControl("txtDetails"), TextBox)
strSQL = "INSERT INTO Product(ProID,ProductName,Price,Image,Details) " & _
" VALUES ('" & txtProID.Text & "','" & txtProductName.Text & "','" & txtPrice.Text & "' " & _
"','" & txtImage.Text & "','" & txtDetails.Text & "') "
objCmd = New Data.OleDb.OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End If
End Sub
Sub GridView1_RowDataBound(ByVal source As Object, ByVal e As GridViewRowEventArgs)
'*** Edit ***'
If e.Row.RowType = DataControlRowType.DataRow Then
'*** ProID ***'
Dim txtProID As TextBox = CType(e.Row.FindControl("txtEdittxtProID"), TextBox)
If Not IsNothing(txtProID) Then
txtProID.Text = e.Row.DataItem("ProID")
End If
'*** ProductName ***'
Dim txtProductName As TextBox = CType(e.Row.FindControl("txtEditProductName"), TextBox)
If Not IsNothing(txtProductName) Then
txtProductName.Text = e.Row.DataItem("ProductName")
End If
'*** Price ***'
Dim txtPrice As TextBox = CType(e.Row.FindControl("txtEditPrice"), TextBox)
If Not IsNothing(txtPrice) Then
txtPrice.Text = e.Row.DataItem("Price")
End If
'*** Image ***'
Dim txtImage As TextBox = CType(e.Row.FindControl("txtEditImage"), TextBox)
If Not IsNothing(txtImage) Then
txtImage.Text = e.Row.DataItem("Image")
End If
'*** Details ***'
Dim txtDetails As TextBox = CType(e.Row.FindControl("txtEditDetails"), TextBox)
If Not IsNothing(txtDetails) Then
txtDetails.Text = e.Row.DataItem("Details")
End If
End If
End Sub
Sub modUpdateCommand(ByVal s As Object, ByVal e As GridViewUpdateEventArgs)
'*** ProID ***'
Dim txtProID As TextBox = CType(GridView1.Rows(e.RowIndex).FindControl("txtEditProID"), TextBox)
'*** ProductName ***'
Dim txtProductName As TextBox = CType(GridView1.Rows(e.RowIndex).FindControl("txtEditProductName"), TextBox)
'*** Price ***'
Dim txtPrice As TextBox = CType(GridView1.Rows(e.RowIndex).FindControl("txtEditPrice"), TextBox)
'
'*** Image ***'
Dim txtImage As TextBox = CType(GridView1.Rows(e.RowIndex).FindControl("txtEditImage"), TextBox)
'*** Details***'
Dim txtDetails As TextBox = CType(GridView1.Rows(e.RowIndex).FindControl("txtEditDetails"), TextBox)
strSQL = "UPDATE Product SET ProID = '" & txtProID.Text & "' " & " ,ProductName = '" & txtProductName.Text & "' " & _
" ,Price = '" & txtPrice.Text & "' " & _
" ,Image = '" & txtImage.Text & "' " & _
" ,Details = '" & txtDetails.Text & "' " & _
" WHERE ProID = '" & GridView1.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New Data.OleDb.OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
GridView1.EditIndex = -1
GridView1.ShowFooter = True
BindData()
End Sub
Private Sub InitializeComponent()
Me.backgroundWorker1 = New System.ComponentModel.BackgroundWorker
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("Maintain.aspx")
End Sub
End Class
code behind page:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="update.aspx.vb" Inherits="layout2" %>
Gomathi PalaniswamyPosted Oct 20, 2010, 9:07 AM
check your code..its updating,
Posted Oct 17, 2010, 11:36 AM
Bryian TanPosted Oct 17, 2010, 11:27 AM
Please read the post carefully. The modUpdateCommand subroutine should look like this:
Sub modUpdateCommand(ByVal s As Object, ByVal e As GridViewUpdateEventArgs)
GridView1.EditIndex = -1
GridView1.ShowFooter = True
BindData()
End Sub
razaleigh harisPosted Oct 17, 2010, 10:52 AM
i try the solution but when view in browser it give this error
Server Error in '/JomMakan Fyp3' Application.
No value given for one or more required parameters.
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.Data.OleDb.OleDbException: No value given for one or more required parameters.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.do u have any sampel code for update,deleting,insert into datagridview connecting with acess.mdb??? because i want it as my references..please because i want make a new for this one.this my final year project...pless help me..on thursyday i have submin
razaleigh harisPosted Oct 17, 2010, 10:43 AM
i cant find the the solution..
razaleigh harisPosted Oct 17, 2010, 10:42 AM
i cant find the the solution..
Gomathi PalaniswamyPosted Oct 17, 2010, 8:30 AM
The problem is the text box not getting value from data grid ..will check and tel..
razaleigh harisPosted Oct 17, 2010, 6:49 AM
i want that code exmple..for update and delete... please help me i begging anyone..please... my coding always give an error.
razaleigh harisPosted Oct 17, 2010, 6:45 AM
Source Error:
Line 169: Dim txtDetails As TextBox = CType(GridView1.Rows(e.RowIndex).FindControl("txtEditDetails"), TextBox) Line 170: Line 171: strSQL = "UPDATE Product SET ProID = '" & txtProID.Text & "' " & " ,ProductName = '" & txtProductName.Text & "' " & _ Line 172: " ,Price = '" & txtPrice.Text & "' " & _ Line 173: " ,Image = '" & txtImage.Text & "' " & _then it getting worst when i cant delete too... it give error like
No value given for one or more required parameters.
razaleigh harisPosted Oct 17, 2010, 4:59 AM
my question is... when i view in browser my grid view,,just only can delete the data but cant update... how i can solve this problem...above is my coding and also the form.. i hope u can vie and solve my problem..
Bryian TanPosted Oct 17, 2010, 3:04 AM
All the code in the modUpdateCommand Subroutine is unnecessary because the GridView was created based on the AccessDataSource.
Sub modUpdateCommand(ByVal s As Object, ByVal e As GridViewUpdateEventArgs)
GridView1.EditIndex = -1
GridView1.ShowFooter = True
BindData()
End Sub
I'm not sure what you trying to achieve with this line and it not working correctly.
UpdateCommand="UPDATE [Product] SET [ProductName] = ?, [Price] = ?, [Image] = ?, [Details] = ? WHERE (([ProID] = ?) OR ([ProID] IS NULL AND ? IS NULL)) AND (([ProductName] = ?) OR ([ProductName] IS NULL AND ? IS NULL)) AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL)) AND (([Image] = ?) OR ([Image] IS NULL AND ? IS NULL)) AND (([Details] = ?) OR ([Details] IS NULL AND ? IS NULL))"
We can trim it down to:
UpdateCommand="UPDATE [Product] SET [ProductName] = ?, [Price] = ?, [Image] = ?, [Details] = ? WHERE ([ProID] = ?) "
Make those changes and update the GridView item again and let us know the result.
These article might be useful:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx
http://msdn.microsoft.com/en-us/library/ms972948.aspx
Gomathi PalaniswamyPosted Oct 17, 2010, 2:29 AM
make your post clear.what is your question??