Solved

ASP.net Insert data into an Access table VB.net

Posted on 2011-03-25
6
506 Views
Last Modified: 2012-05-11
Hi

In ASP.net 4 I am using the code below to try to insert data into an Access table.
What VB code do I use to execute the Insert SQL statement
Protected Sub oInsert()
        '// define a connection to the database
        Dim cs As String = ConfigurationManager.ConnectionStrings("WhatEverNameYouWant").ConnectionString
        cs = cs.Replace("App_Data\GC.accdb", Server.MapPath("App_Data\GC.accdb"))

        Dim cn As New OleDbConnection(cs)


        Dim sFirstName As String
        sFirstName = "Test1"
        Dim sLastName As String
        sLastName = "Test2"
        Dim sEmail As String
        sEmail = "Test3"

        'If sEmail <> "murbro9@yahoo.com" Then
        'Me.TextBox3.Text = "#INVALID EMAIL#"
        'Exit Sub
        'End If

        Dim sSQL As String
        sSQL = "INSERT INTO Test ( [A], [B], [C] ) SELECT '" & sFirstName & "' AS Expr1, '" & sLastName & "' AS Expr2, '" & sEmail & "' AS Expr3"

        '// define the sql statement to execute
        Dim cmd As New OleDbCommand(sSQL, cn)

        Try


            '// open the connection
            cn.Open()

            '// execute the sql statement
            'Using reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

            ' While reader.Read()
            '// this loops through all of the returned records
            'Response.Write("blah")
            'End While

            'GridView1.DataSource = reader
            'GridView1.DataBind()



            'End Using

        Catch ex As Exception
            Response.Write(ex.Message)

        Finally
            If cn.State <> ConnectionState.Closed Then
                cn.Close()
            End If
        End Try
    End Sub

Open in new window

0
Comment
Question by:murbro
6 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 200 total points
ID: 35214357
you need to use

          cmd.ExecuteNonQuery()

try this code
Protected Sub oInsert()
        '// define a connection to the database
        Dim cs As String = ConfigurationManager.ConnectionStrings("WhatEverNameYouWant").ConnectionString
        cs = cs.Replace("App_Data\GC.accdb", Server.MapPath("App_Data\GC.accdb"))

        Dim cn As New OleDbConnection(cs)


        Dim sFirstName As String
        sFirstName = "Test1"
        Dim sLastName As String
        sLastName = "Test2"
        Dim sEmail As String
        sEmail = "Test3"

        'If sEmail <> "murbro9@yahoo.com" Then
        'Me.TextBox3.Text = "#INVALID EMAIL#"
        'Exit Sub
        'End If

        Dim sSQL As String
        sSQL = "INSERT INTO Test ( [A], [B], [C] ) SELECT '" & sFirstName & "' AS Expr1, '" & sLastName & "' AS Expr2, '" & sEmail & "' AS Expr3"

        '// define the sql statement to execute
        Dim cmd As New OleDbCommand(sSQL, cn)

        Try

          cmd.ExecuteNonQuery()

    
        Catch ex As Exception
            Response.Write(ex.Message)

        Finally
            If cn.State <> ConnectionState.Closed Then
                cn.Close()
            End If
        End Try
    End Sub

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
ID: 35214358
You just need to call:
cmd.ExecuteNonQuery()

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 35214488
If you're in need of a full tutorial regarding .net and access database, read this:

http://msdn.microsoft.com/en-us/library/ms971485.aspx

Arun
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35215119
Well if we're being picky then pratima_mcs' solution will throw an exception because the connection is never opened.
0
 

Author Closing Comment

by:murbro
ID: 35216741
thanks
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 77
When should I use a controller? 3 34
encoding an href string 2 20
C# LINQ ForEach() question 6 28
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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