locked
Inserting data into MS Accsss RRS feed

  • Question

  • User-1793215261 posted

    Hello

    I am unable to insert simple data (3 fields: name, email, password) into my MS Access database (in my App_Data folder) when I am in VS on the desktop.

    When I click on 'start debugging', VS tell me 'validation complete', and my Register.aspx page loads. However, when I complete the registration form the data doesn't go anywhere - nor does the form.

    I can see from a similar past project I have done, I have these namespaces:

    Imports Microsoft.AspNet.Identity
    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports Microsoft.AspNet.Identity.Owin
    Imports System
    Imports System.Linq
    Imports System.Web
    Imports System.Web.UI
    Imports System.Data.OleDb
    

    In this current project, I only have:

    Imports System 
    Imports System.Linq 
    Imports System.Web 
    Imports System.Web.UI 
    Imports System.Data.OleDb

    When I try to include:

    Imports Microsoft.AspNet.Identity 
    Imports Microsoft.AspNet.Identity.EntityFramework 
    Imports Microsoft.AspNet.Identity.Owin

    in this current project, I get green underlines for all three namespaces each of which tells me 'no correction suggestions' but a 'message' appears alongside 'doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member'.

    Are those three namespaces necessary to connect to my MS Access database or can I remove them? I am just trying to investigate why I can't connect to the mdb or insert data into it.

    Thanks.

    Friday, December 4, 2015 12:24 PM

All replies

  • User-821857111 posted

    Bluenose

    Are those three namespaces necessary to connect to my MS Access database

    No. But their inclusion won't stop you inserting data into a database. You should show the code you are trying to get to work.

    Friday, December 4, 2015 12:46 PM
  • User-1793215261 posted

    Thanks, Mike, for your reply.

    I have this in my register.aspx.vb file:

     Protected Sub contact_submitted_Click(sender As Object, e As EventArgs)
    
    
            Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    
                Dim Sql As String = "INSERT INTO university (username,[password],strEmail) VALUES (@username,@password,@strEmail)"
                Dim cmd As New OleDbCommand(Sql, conn)
                conn.Open()
                cmd.Parameters.AddWithValue("@username", username.Text)
                cmd.Parameters.AddWithValue("@password", password.Text)
                cmd.Parameters.AddWithValue("@strEmail", strEmail.Text)
                cmd.ExecuteNonQuery()
                conn.Close()
    
            End Using
    
            Dim target = String.Format("~/success.aspx?Name={0}", username.Text)
            ' Perform your Redirect '
            Response.Redirect(target, True)
        End Sub
    
    End Class
    
    
    
    
    
    

    and in my MS Access db I have three columns, username, password, and strEmail.

    When the user successfully registers, he is redirected to a 'success' page, and the success.aspx.vb file looks like this:

    Public Class success
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Request("Name") IsNot Nothing Then
                ' It exists, so set your label (and a trailing comma) to display your name '
                Name.Text = String.Format("{0}, ", Request("Name"))
            End If
    
        End Sub
    
    End Class

    Many thanks again!
     

    Friday, December 4, 2015 3:53 PM
  • User-821857111 posted

    When you click the contact button, do you get redirected to success.aspx? If so, are you certain you are looking at the right database for data? Have you refreshed the table view?

    Friday, December 4, 2015 4:21 PM
  • User-1793215261 posted

    No, when I click 'Send' to register, nothing happens. I am not redirected. The form stays where it is as does the data I have entered. Nothing is entered in the database. I don't get any errors when I do the above, either. 

    Friday, December 4, 2015 4:24 PM
  • User-821857111 posted

    Then your button click event handler isn't wired up to the button.

    Protected Sub contact_submitted_Click(sender As Object, e As EventArgs) Handles contact_submitted.Click

    Friday, December 4, 2015 4:34 PM
  • User-1793215261 posted

    That gives me a blue underline, Mike, here: contact_submitted with the message 'Handles clause requires a WithEvents variable defined in the containing type or one of its base types'

    Friday, December 4, 2015 4:41 PM
  • User-821857111 posted

    Can you show the relevant code form your aspx?

    Friday, December 4, 2015 9:35 PM
  • User-1793215261 posted

    Yes, this is the button in register.aspx:

    <input type="submit" name="contact_submitted" value="Send" onclick="javascript: WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(contact_submitted, true, false, false));" id="contact_submitted" class="submit" />

    Friday, December 4, 2015 10:07 PM
  • User-821857111 posted

    Any particular reason why you are not using an asp:Button?

    Saturday, December 5, 2015 8:47 PM
  • User-1793215261 posted

    No, not really. The button is called 'contact_submitted'. It should work whatever I call it, shouldn't it?

    I have a contact form here: http://www.dimadayoub.net/contact.aspx and that 'Send' button, with the same name, works.

    This is the same site. The button above refers to the contact form, while this post, inserting data into MS Access, refers to a new user registering with the site:

    http://www.dimadayoub.net/register.aspx

    Maybe call this Register.aspx button by a different name if you think the problem is with the button?

    Thanks again.

    Saturday, December 5, 2015 9:18 PM
  • User753101303 posted

    Hi,

    What if passing the 'contact_submitted' string to your postback options (currently this is not a string but a variable name maybe unintialized).

    Or as pointed already just use what ASP.NET offers so that you don't have issues caused by writing yourself what ASP.NET can generate for you.

    Sunday, December 6, 2015 8:20 PM
  • User-1793215261 posted

    Thanks for your reply.

    I will follow your advice and do something like this in the hope it will resolve the problem:

     <asp:Button ID="btnReg" runat="server" Text="Send" CssClass="submit" />   

    where btnReg = the Registration Button.

    What I am looking for in VS is something like value="Send" or class="submit" that will allow the button to function. What is the default, please (I am just trying to minimise any room for future errors).

    Thanks! 

    Sunday, December 6, 2015 8:47 PM
  • User-821857111 posted

    I will follow your advice and do something like this in the hope it will resolve the problem:

     <asp:Button ID="btnReg" runat="server" Text="Send" CssClass="submit" />   

    Then you need to make sure the Handles clause references the correct ID. In my previous example, the button had a different ID.

    Sunday, December 6, 2015 9:06 PM
  • User-1793215261 posted

    I have done this, Mike:

     Protected Sub btnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click

    Does that seem correct?

    I am not too sure what to do about the 'Send' value in the button itself:

     <asp:Button ID="btnReg" runat="server" Text="Send" CssClass="submit" />  

    At the moment, it's not doing anything, is it?

    Sunday, December 6, 2015 9:22 PM
  • User-1716253493 posted
        Protected Sub btnReg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReg.Click
            Try
                Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
                    Dim Sql As String = "INSERT INTO university (username,[password],strEmail) VALUES (@username,@password,@strEmail)"
                    Dim cmd As New OleDbCommand(Sql, conn)
                    conn.Open()
                    cmd.Parameters.AddWithValue("@username", username.Text)
                    cmd.Parameters.AddWithValue("@password", password.Text)
                    cmd.Parameters.AddWithValue("@strEmail", strEmail.Text)
                    cmd.ExecuteNonQuery()
                    conn.Close()
                End Using
    
                Dim target = String.Format("~/success.aspx?Name={0}", username.Text)
                Response.Redirect(target, True)
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
        End Sub
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Name.Text = String.Format("{0}, ", Request.QueryString("Name"))
        End Sub



    Monday, December 7, 2015 3:08 AM
  • User-1793215261 posted

    Hello oned_gk

    Many thanks for your reply.

    I have almost the same as you have kindly offered:

    Protected Sub btnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click
    
    
    
            Using conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    
                Dim Sql As String = "INSERT INTO university (username,[password],strEmail) VALUES (@username,@password,@strEmail)"
                Dim cmd As New OleDbCommand(Sql, conn)
                conn.Open()
                cmd.Parameters.AddWithValue("@username", username.Text)
                cmd.Parameters.AddWithValue("@password", password.Text)
                cmd.Parameters.AddWithValue("@strEmail", strEmail.Text)
                cmd.ExecuteNonQuery()
                conn.Close()
    
            End Using
    
            Dim target = String.Format("~/success.aspx?Name={0}", username.Text)
            ' Perform your Redirect '
            Response.Redirect(target, True)
        End Sub
    

    Except I didn't have your Try/Catch ex As Exception!

    Should it be what you have or should it be this:

     Catch ex As Exception
    
                Console.WriteLine(ex.Message)
    

    Thank you again for your time!

    Monday, December 7, 2015 6:56 AM