razaleigh haris

razaleigh haris

  • NA
  • 13
  • 14.8k

Why i cant update data in grid view only ,and i only can delete data. please help me... i used asp.net with acess.mdb

Oct 16 2010 12:37 PM
here the code:




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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body style="background-image: url(image/lll.jpg); background-color: transparent">
    <form id="form1" runat="server">
        <br />
        <asp:Label ID="Label1" runat="server" Text="Update Menu"></asp:Label><br />
        <asp:Button ID="Button1" runat="server" PostBackUrl="~/Maintain.aspx" Text="Back" /><br />
        <br />
        <br />
<asp:GridView id="GridView1" runat="server" AutoGenerateColumns="False"
ShowFooter="True"
DataKeyNames="ProID"
OnRowEditing="modEditCommand"
OnRowCancelingEdit="modCancelCommand"
OnRowDeleting="modDeleteCommand"
OnRowUpdating="modUpdateCommand"
OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound" DataSourceID="AccessDataSource1" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" style="left: 276px; position: absolute; top: 134px" Width="576px">
 
            <Columns>
                <asp:BoundField DataField="ProID" HeaderText="ProID" ReadOnly="True" SortExpression="ProID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                <asp:BoundField DataField="Details" HeaderText="Details" SortExpression="Details" />
                <asp:ImageField DataImageUrlField="Image" DataImageUrlFormatString="~\image\{0}">
                </asp:ImageField>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
 
       
       
       
       
       
              
            </Columns>
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/login.mdb"
            SelectCommand="SELECT * FROM [Product]" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [Product] 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))" InsertCommand="INSERT INTO [Product] ([ProID], [ProductName], [Price], [Image], [Details]) VALUES (?, ?, ?, ?, ?)" OldValuesParameterFormatString="original_{0}" 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))">
            <DeleteParameters>
                <asp:Parameter Name="original_ProID" Type="String" />
                <asp:Parameter Name="original_ProductName" Type="String" />
                <asp:Parameter Name="original_Price" Type="Decimal" />
                <asp:Parameter Name="original_Image" Type="String" />
                <asp:Parameter Name="original_Details" Type="String" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="Price" Type="Decimal" />
                <asp:Parameter Name="Image" Type="String" />
                <asp:Parameter Name="Details" Type="String" />
                <asp:Parameter Name="original_ProID" Type="String" />
                <asp:Parameter Name="original_ProductName" Type="String" />
                <asp:Parameter Name="original_Price" Type="Decimal" />
                <asp:Parameter Name="original_Image" Type="String" />
                <asp:Parameter Name="original_Details" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="ProID" Type="String" />
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="Price" Type="Decimal" />
                <asp:Parameter Name="Image" Type="String" />
                <asp:Parameter Name="Details" Type="String" />
            </InsertParameters>
        </asp:AccessDataSource>
       
   
</form>
</body>
</html>


Attachment: update menu.rar

Answers (12)