ASP insert into SQL with VB


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: ASP insert into SQL with VB

  1. #1
    Join Date
    Oct 2005
    Posts
    133

    ASP insert into SQL with VB

    i foundthis code on DEVX, and have copied into my code, i used the following as my connection string info

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=server=spumbu\sqlexpress;database=oss;Trusted_Connection=Yes")

    and my strsql string was

    INSERT INTO WebRequestCat(Name, EmailAddress) VALUES (N'txt1', N'txt2')

    although when testing it just hangs on load . ? any ideas please

    as i am now officially dumbstruck lol .?

    Dim strSQL as string = "INSERT INTO TableName(Field1, Field2) Values(Val1, Val2)"
    'Where val1 and val2 are parameters
    Dim objConnection as SQLConnection("your connection string here")
    Dim objCommand as SQLCommand(strSQL, objConnection)

    objConnection.Open()
    objCommand.Parameters.Add("@Val1", TextBox1.Text)
    objCommand.Parameters.Add("@Val2", TextBox2.Text)

    objCommand.ExecuteNonQuery()
    objConnection.Close

    Thanks in advance
    Rob

  2. #2
    Join Date
    Oct 2005
    Posts
    133
    i think there are some references that i need to add, as since i sort of into VB6 i do know that there is always a reference that needs adding

    please any help is always apreciated

    Rob

  3. #3
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi Rob,

    have a look at this page - it has a good example of what you are trying to do.

    http://www.vistadb.net/vistadb3/tuto...rtRows-VB.aspx
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

  4. #4
    Join Date
    Oct 2005
    Posts
    133
    thanks for the quick and helpful response , and i will try this when i get home tonight,

    by the way i like your moto about walking a mile in their shoes lol

    quick question, is there any benefits in using DDA in comparison to adodb .?

    thanks
    Rob

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    If you are using any form of .NET technology, you should not be using adodb for anything.

    For .NET database work, always use ADO.NET
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  6. #6
    Join Date
    Oct 2005
    Posts
    133
    Hi All

    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim ra As Integer
    myConnection = New SqlConnection("server=gbrltsxpp-12332\sqlexpress;uid=sa;pwd=mikki1;database=test")
    myConnection.Open()
    myCommand = New SqlCommand("Insert into test1 'ID','Data1','Dat2' values 'textbox1.text','textbox2.text ','textbox3.text', myConnection)
    ra = myCommand.ExecuteNonQuery()
    MsgBox("Completed")
    myConnection.Close()

    with the above code, 'mycommand' seems to throw up errors about the textbox not being in context,

    i have tried with colons, comma's and nothing at all, however it tells me the same thing for allof them, can anyone see what i might be missing

    thanks very much in advance for all your help

    Rob

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    You are missing parentheses around your VALUES clause and field list. Also, field names do not need single quotes
    Code:
    Dim sSQL As String
    sSQL = "INSERT INTO test1 (ID, Data1, Dat2) "  'id, data1 and dat2 should be the names of fields in your table
    sSQL = sSQL & "VALUES ("' & textbox1.text & "', '" & textbox2.text & "', '' & textbox1.text & "')"
    I broke it up for readibility.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  8. #8
    Join Date
    Oct 2005
    Posts
    133
    thanks mate for your help, that worked like a treat, but if i may, i wish to ask another question please

    my behind the scene code is as follows
    Imports System.Data.SqlClient

    Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim ra As Integer
    myConnection = New SqlConnection("server=gbrltsxpp-12332\sqlexpress;uid=sa;pwd=mikki1;database=test")
    myConnection.Open()
    myCommand = New SqlCommand("INSERT INTO test1 (ID, Data1, Data2)VALUES ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox1.Text & "')", myConnection)
    ra = myCommand.ExecuteNonQuery()



    MsgBox("Completed")
    myConnection.Close()
    End Sub
    End Class

    however as you can see my code sits under the Button1_click method but runs as soon as the page starts, is there anyway to stop this from happenening .?

    thanks in advance
    Rob

  9. #9
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Does the button have focus when the page opens?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  10. #10
    Join Date
    Oct 2005
    Posts
    133
    i dont want to sound like i know much as i dont really besides vb6, and in order to set things in order i used to use Tabindex and Tapstop properties,

    how would i go about looking into finding out if it is or not .?

    thanks in advance
    Rob

Similar Threads

  1. Replies: 0
    Last Post: 01-03-2007, 12:02 PM
  2. ASP vs. VB objects
    By Chad in forum ASP.NET
    Replies: 8
    Last Post: 06-25-2001, 01:37 AM
  3. Replies: 0
    Last Post: 04-24-2001, 09:55 PM
  4. DevWizard 2001 - For VB, ASP, and SQL Developers
    By Dave V in forum vb.announcements
    Replies: 0
    Last Post: 04-08-2001, 03:01 PM
  5. Multi-row calculations
    By Bob Hines in forum Database
    Replies: 7
    Last Post: 04-27-2000, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center