0

I have a gridview with access and built with "select","edit" & "delete", the outside under gridview with the "add","cancel" buttons.


details:

select edit delet id name etc.


Textbox1
Textbox2

add cancel

ALL THE BUILT SELECT,EDIT,DELETE WRER WORKING, ONLY THE "ADD" BUTTON WAS NOT WORKING. So, I rebind the gridview with access - GOT THE DATASOURCE/DATASOURCEID PROBLEM, I delete the the datasourceid in the html, the "add" button actually is working and will refresh the gridview.

details:

&nbsp;<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
AutoGenerateColumns="False" DataKeyNames="id" 
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="779px">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />&nbsp;<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="AccessDataSource1" 
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="779px">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGridView()
        End If

    End Sub
    Private Sub BindGridView()
        ' Code to retrieve records from database and fill it in a DataTable and Bind it to GridView
        Dim dt As DataTable = New DataTable()
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Sysdep", conn)
        conn.Open()
        da.Fill(dt)
        da.Dispose()
        conn.Close()

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        Dim sqlinsert As String

        sqlinsert = "INSERT INTO sysdep (sysaccount,syspw)" & _
          "VALUES(@sysaccount, @syspw)"

        Dim cmd As New OleDbCommand(sqlinsert, con1)

        cmd.Parameters.Add(New OleDbParameter("@sysaccount", TextBox1.Text))
        cmd.Parameters.Add(New OleDbParameter("@syspw", TextBox2.Text))

        Try
            con1.Open()
            cmd.ExecuteNonQuery()
        Catch ex As OleDbException
            MsgBox(ex.Message, MsgBoxStyle.Critical,"web")
        Catch ex As InvalidOperationException
            MsgBox(ex.Message, MsgBoxStyle.Critical,"web")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical,"web")
        End Try

        con1.Close()
        BindGridView()

    End Sub

BUT NOW THE BUILT THE EDIT,DELETE ARE NOT WORKING - ERRORS,GRIDVIEW1 NOT HANDLE THE ROWEDITING, ROWDELETING,how to fix them?

Thanks.

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by dnanetwork
0

I have a gridview with access and built with "select","edit" & "delete", the outside under gridview with the "add","cancel" buttons.


details:

select edit delet id name etc.


Textbox1
Textbox2

add cancel

ALL THE BUILT SELECT,EDIT,DELETE WRER WORKING, ONLY THE "ADD" BUTTON WAS NOT WORKING. So, I rebind the gridview with access - GOT THE DATASOURCE/DATASOURCEID PROBLEM, I delete the the datasourceid in the html, the "add" button actually is working and will refresh the gridview.

details:

&nbsp;<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
AutoGenerateColumns="False" DataKeyNames="id" 
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="779px">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />&nbsp;<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="AccessDataSource1" 
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="779px">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGridView()
        End If

    End Sub
    Private Sub BindGridView()
        ' Code to retrieve records from database and fill it in a DataTable and Bind it to GridView
        Dim dt As DataTable = New DataTable()
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Sysdep", conn)
        conn.Open()
        da.Fill(dt)
        da.Dispose()
        conn.Close()

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        Dim sqlinsert As String

        sqlinsert = "INSERT INTO sysdep (sysaccount,syspw)" & _
          "VALUES(@sysaccount, @syspw)"

        Dim cmd As New OleDbCommand(sqlinsert, con1)

        cmd.Parameters.Add(New OleDbParameter("@sysaccount", TextBox1.Text))
        cmd.Parameters.Add(New OleDbParameter("@syspw", TextBox2.Text))

        Try
            con1.Open()
            cmd.ExecuteNonQuery()
        Catch ex As OleDbException
            MsgBox(ex.Message, MsgBoxStyle.Critical,"web")
        Catch ex As InvalidOperationException
            MsgBox(ex.Message, MsgBoxStyle.Critical,"web")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical,"web")
        End Try

        con1.Close()
        BindGridView()

    End Sub

BUT NOW THE BUILT THE EDIT,DELETE ARE NOT WORKING - ERRORS,GRIDVIEW1 NOT HANDLE THE ROWEDITING, ROWDELETING,how to fix them?

Thanks.

Actually I got them all except the gridview1_rowupdating.

[Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        BindGridView()
    End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        BindGridView()
    End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

        Dim sqldelete As String
        Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")

        sqldelete = "Delete From sysdep Where id=" & GridView1.DataKeys(e.RowIndex).Value.ToString()
        Dim cmd2 As New OleDbCommand(sqldelete, con1)

        Try
            con1.Open()
            cmd2.ExecuteNonQuery()
        Catch ex As OleDbException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "web")
        Catch ex As InvalidOperationException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "web")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "web")
        End Try

        con1.Close()
        BindGridView()
    End Sub

Edited by needanswer: n/a

0

I will try this, thanks.

Actually, really not for sure how to do the gridview1_rowupdating, so far the detail:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataKeyNames="id"  
            BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
            CellPadding="3" GridLines="Vertical">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black"/>
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="True" 
                            CommandName="Update" Text="Update" />
                        &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" 
                            CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="False" 
                            CommandName="Edit" Text="Edit" />
                        &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" 
                            CommandName="Select" Text="Select" />
                        &nbsp;<asp:Button ID="Button3" runat="server" CausesValidation="False" 
                            CommandName="Delete" Text="Delete"  OnClientClick="return confirm('Are you sure?');"/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                    ReadOnly="True" SortExpression="id" Visible="False" />
                <asp:BoundField DataField="sysaccount" HeaderText="sysaccount" 
                    SortExpression="sysaccount" />
                <asp:BoundField DataField="syspw" HeaderText="syspw" SortExpression="syspw" />
                <asp:BoundField DataField="log" HeaderText="log" SortExpression="log" />
            </Columns>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="C:\Users\user\Desktop\honor.mdb" 
            DeleteCommand="DELETE FROM [sysdep] WHERE [id] = @id" 
            InsertCommand="INSERT INTO [sysdep] ([id], [sysaccount], [syspw], [log]) VALUES (@id, @sysaccount, @syspw, @log)" 
            SelectCommand="SELECT [id], [sysaccount], [syspw], [log] FROM [sysdep]" 
            UpdateCommand="UPDATE [sysdep] SET [sysaccount] = @sysaccount, [syspw] = @syspw, [log] = @log WHERE [id] = @id">
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="sysaccount" Type="String" />
                <asp:Parameter Name="syspw" Type="String" />
                <asp:Parameter Name="log" Type="DateTime" />
                <asp:Parameter Name="id" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="id" Type="Int32" />
                <asp:Parameter Name="sysaccount" Type="String" />
                <asp:Parameter Name="syspw" Type="String" />
                <asp:Parameter Name="log" Type="DateTime" />
            </InsertParameters>
        </asp:AccessDataSource>

This is origional from the vb 2008 express.

Dim sqlupdate As String
        Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
        sqlupdate = "UPDATE sysdep SET sysaccount= @sysaccount, syspw= @syspw WHERE id= @id"

        Dim cmd1 As New OleDbCommand(sqlupdate, con1)

        Dim sysaccount As String
        Dim syspw As String
        Dim id As Integer

        cmd1.Parameters.Add(New OleDbParameter("@sysaccount", TextBox2.Text)) ....origional textbox on the vb 2008 express
        cmd1.Parameters.Add(New OleDbParameter("@syspw", TextBo3.Text))....origional textbox on the vb 2008 express
        cmd1.Parameters.Add(New OleDbParameter("@id", TextBox1.Text))....origional textbox on the vb 2008 express

        Try
            con1.Open()
            cmd1.ExecuteNonQuery()
        Catch ex As OleDbException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
        Catch ex As InvalidOperationException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
        End Try

        con1.Close()

        BindGridView()
    End Sub

So far everything is working, except the gridview1_rowupdating.

Edited by needanswer: n/a

0

Make this lil modification..

con1.Close()
[B]
GridView1.EditIndex = -1[/B]
BindGridView()

hope that works out 4 you..!

Edited by dnanetwork: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.