?
Solved

Submit Button that Inserts to Access Db, validates and displays a confirmation

Posted on 2005-04-05
3
Medium Priority
?
318 Views
Last Modified: 2012-05-05
Hi Experts,

I'm a newbie using ASP.NET. I'm using VB and web matrix (for what it's worth).

I've got a page that has a "Submit" button which runs an INSERT query into an ACCESS db.  Clicking submit inserts a new record into a dB and that seems to be working.  

My problem:  Clicking submit inserts the record into the db, but it doesn't clear the fields, validate, or give any indication on the page that the record was updated.

I'm looking for straight-forward code that will validate the info, insert the record, and show a confirmation message to the user.

Here's my OnClick event.


 Sub SubmitSolution(sender As Object, e As EventArgs)
     

        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
           "Data Source=C:\matrix\db1.mdb"

        Dim dbConnection As New OleDbConnection(connectionString)
        dbConnection.Open()

        Dim commandString As String = "INSERT INTO tblSolutions(CategoryID, SolutionTitle, Solution) " & _
           "Values(@CategoryID, @SolutionTitle, @Solution)"

        Dim dbCommand As New OleDbCommand(commandString, dbConnection)

        Dim CategoryIDParam As New OleDbParameter("@CategoryID", OleDbType.VarChar, 10)
        CategoryIDParam.Value = DropDownList1.SelectedItem.Value
        dbCommand.Parameters.Add(CategoryIDParam)

        Dim SolutionTitleParam As New OleDbParameter("@SolutionTitle", OleDbType.VarChar, 100)
        SolutionTitleParam.Value = TextBox4.Text
        dbCommand.Parameters.Add(SolutionTitleParam)

        Dim SolutionParam As New OleDbParameter("@Solution", OleDbType.VarChar, 100)
        SolutionParam.Value = TextBox1.Text
        dbCommand.Parameters.Add(SolutionParam)


        dbCommand.ExecuteNonQuery()
        dbConnection.Close()

    End Sub


Thanks for your help.

I'm looking for specific code examples, tx.
0
Comment
Question by:TCantrell
3 Comments
 
LVL 9

Accepted Solution

by:
stengelj earned 1000 total points
ID: 13711747
Try this...

........................................
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script runat="server">
    Sub SubmitSolution(sender As Object, e As EventArgs)    
        Try
            Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
               "Data Source=C:\matrix\db1.mdb"
   
            Dim dbConnection As New OleDbConnection(connectionString)
            dbConnection.Open()
   
            Dim commandString As String = "INSERT INTO tblSolutions(CategoryID, SolutionTitle, Solution) " & _
               "Values(@CategoryID, @SolutionTitle, @Solution)"
   
            Dim dbCommand As New OleDbCommand(commandString, dbConnection)
   
            Dim CategoryIDParam As New OleDbParameter("@CategoryID", OleDbType.VarChar, 10)
            CategoryIDParam.Value = DropDownList1.SelectedItem.Value
            dbCommand.Parameters.Add(CategoryIDParam)
   
            Dim SolutionTitleParam As New OleDbParameter("@SolutionTitle", OleDbType.VarChar, 100)
            SolutionTitleParam.Value = TextBox4.Text
            dbCommand.Parameters.Add(SolutionTitleParam)
   
            Dim SolutionParam As New OleDbParameter("@Solution", OleDbType.VarChar, 100)
            SolutionParam.Value = TextBox1.Text
            dbCommand.Parameters.Add(SolutionParam)
   
   
            dbCommand.ExecuteNonQuery()
            dbConnection.Close()
            myForm.Visible = False
            lbl_Confirm.Text = "Form successfully submitted"
       
        Catch ex As Exception
            lbl_Confirm.Text = "There was an error adding your information to the database"
       
        End Try

    End Sub

</script>
</head>
<body>
<form runat="server" id="myForm">
    <asp:TextBox ID="TextBox1" runat="server" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
        ControlToValidate="TextBox1"
        ErrorMessage="Required"
        Display="Dynamic" runat="server">
    </asp:RequiredFieldValidator>
    <br>
    <asp:TextBox ID="TextBox4" runat="server" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
        ControlToValidate="TextBox4"
        ErrorMessage="Required"
        Display="Dynamic" runat="server">
    </asp:RequiredFieldValidator>
    <br>
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem value="">[add item]</asp:ListItem>
        <asp:ListItem value="1">item1</asp:ListItem>
        <asp:ListItem value="2">item2</asp:ListItem>
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3"
        ControlToValidate="DropDownList1"
        ErrorMessage="Required"
        Display="Dynamic" runat="server">
    </asp:RequiredFieldValidator>
    <br>
    <asp:Button ID="myButton" runat="server" Text="Submit" OnClick="SubmitSolution" />
</form>
<asp:Label ID="lbl_Confirm" runat="server"></asp:Label>
</body>
</html>
0
 
LVL 16

Expert Comment

by:MikeMCSD
ID: 13711753
To validate data, use a RequiredFieldValidator. ex: using a textbox:

<asp:textbox runat="server" id="txtAddress1"></asp:textbox>
<asp:RequiredFieldValidator ControlToValidate="txtAddress1" ID="vAddress1"
        Runat="server"  ErrorMessage="Enter address."></asp:RequiredFieldValidator>


To check for update failure, can use:

Try
     dbCommand.ExecuteNonQuery()
     lblCat.Text = "Update Ok"
     dbConnection.Close()
Catch ex As Exception
      lblCat.Text = "Update Failed"
      dbConnection.Close()
End Try
0
 

Author Comment

by:TCantrell
ID: 13712028
Tx  stengelj

That worked like a charm.

Thanks!

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question