Results 1 to 7 of 7

Thread: Im Confused With This MySQL Query Issue

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Location
    Scotland/Japan
    Posts
    164

    Im Confused With This MySQL Query Issue

    this code is saying theres something wrong with the syntax but it worked last way i used it.....

    vb Code:
    1. Sub useringo()
    2.         Dim conn As MySqlConnection
    3.  
    4.         'connect to DB
    5.         conn = New MySqlConnection()
    6.         conn.ConnectionString = "server=localhost; user id=user; password=pess; database=login"
    7.         'see if connection failed.
    8.         Try
    9.             conn.Open()
    10.         Catch myerror As MySqlException
    11.             MessageBox.Show("Error Connecting to Database: " & myerror.Message)
    12.         End Try
    13.         Dim myAdapter As New MySqlDataAdapter
    14.  
    15.         Dim sqlquery As String
    16.         sqlquery = "select * from userlist Where username='" & My.Settings.unam & "'" & "Insert (name, site, message, about, youtube, myspace, email) VALUES( ?name, ?site, ?message, ?about, ?youtube, ?myspace, ?email)"
    17.         Dim myCommand As New MySqlCommand()
    18.         myCommand.Connection = conn
    19.         myCommand.CommandText = sqlquery
    20.         myCommand.Parameters.AddWithValue("?name", Form4.TextBox1.Text)
    21.         myCommand.Parameters.AddWithValue("?site", Form4.TextBox3.Text)
    22.         myCommand.Parameters.AddWithValue("?message", Form4.TextBox4.Text)
    23.         myCommand.Parameters.AddWithValue("?about", Form4.TextBox5.Text)
    24.         myCommand.Parameters.AddWithValue("?youtube", Form4.TextBox6.Text)
    25.         myCommand.Parameters.AddWithValue("?myspace", Form4.TextBox7.Text)
    26.         myCommand.Parameters.AddWithValue("?email", Form4.TextBox8.Text)
    27.      
    28.         'start query
    29.         myCommand.ExecuteScalar()
    30.         conn.Close()
    31.     End Sub

    the below code is causeing me loads of issues i dunno ware i went wrong it looked like it would work in theory but in practice aprrently not

    vb Code:
    1. Sub getsinfo()
    2.         Dim conn As MySqlConnection
    3.  
    4.         'connect to DB
    5.         conn = New MySqlConnection()
    6.         conn.ConnectionString = "server=localhost; user id=username; password=password; database=login"
    7.         'see if connection failed.
    8.         Try
    9.             conn.Open()
    10.         Catch myerror As MySqlException
    11.             MessageBox.Show("Error Connecting to Database: " & myerror.Message)
    12.         End Try
    13.         Dim myAdapter(8) As MySqlDataAdapter
    14.  
    15.         Dim sqlquery As String
    16.         Dim sqlquery2 As String
    17.         Dim sqlquery3 As String
    18.         Dim sqlquery4 As String
    19.         Dim sqlquery5 As String
    20.         Dim sqlquery6 As String
    21.         Dim sqlquery7 As String
    22.  
    23.         sqlquery = "SELECT name FROM userlist Where Username='" & My.Settings.unam & "'"
    24.         sqlquery2 = "SELECT site FROM userlist Where Username='" & My.Settings.unam & "'"
    25.         sqlquery3 = "SELECT message FROM userlist Where Username='" & My.Settings.unam & "'"
    26.         sqlquery4 = "SELECT about FROM userlist Where Username='" & My.Settings.unam & "'"
    27.         sqlquery5 = "SELECT youtube FROM userlist Where Username='" & My.Settings.unam & "'"
    28.         sqlquery6 = "SELECT myspace FROM userlist Where Username='" & My.Settings.unam & "'"
    29.         sqlquery7 = "SELECT email FROM userlist Where Username='" & My.Settings.unam & "'"
    30.        
    31.         Dim myCommand(8) As MySqlCommand
    32.  
    33.         myCommand(0).Connection = conn
    34.         myCommand(1).CommandText = sqlquery
    35.         myCommand(2).CommandText = sqlquery2
    36.         myCommand(3).CommandText = sqlquery3
    37.         myCommand(4).CommandText = sqlquery4
    38.         myCommand(5).CommandText = sqlquery5
    39.         myCommand(6).CommandText = sqlquery6
    40.         myCommand(7).CommandText = sqlquery7
    41.         'start query
    42.         myAdapter(1).SelectCommand = myCommand(0)
    43.         myAdapter(2).SelectCommand = myCommand(1)
    44.         myAdapter(3).SelectCommand = myCommand(2)
    45.         myAdapter(4).SelectCommand = myCommand(3)
    46.         myAdapter(5).SelectCommand = myCommand(4)
    47.         myAdapter(6).SelectCommand = myCommand(5)
    48.         myAdapter(7).SelectCommand = myCommand(6)
    49.         myAdapter(8).SelectCommand = myCommand(7)
    50.         Dim mydata(6) As String
    51.         mydata(0) = myCommand(1).ExecuteScalar
    52.         mydata(1) = myCommand(2).ExecuteScalar
    53.         mydata(2) = myCommand(3).ExecuteScalar
    54.         mydata(3) = myCommand(4).ExecuteScalar
    55.         mydata(4) = myCommand(5).ExecuteScalar
    56.         mydata(5) = myCommand(6).ExecuteScalar
    57.         mydata(6) = myCommand(7).ExecuteScalar
    58.  
    59.         profile.Label1.Text = mydata(0)
    60.         profile.LinkLabel1.Text = mydata(1)
    61.         profile.Label2.Text = mydata(3)
    62.         profile.TextBox1.Text = mydata(4)
    63.         profile.LinkLabel2.Text = mydata(5)
    64.         profile.LinkLabel3.Text = mydata(6)
    65.         profile.Label3.Text = mydata(7)
    66.     End Sub

    >.< i really need help i have no idea whats wrong with this
    Between the world we see and the things we fear theres a realm of possibility

    D.L.H. Ind.

    We do what we do in out life to make Yours Simpler

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Im Confused With This MySQL Query Issue

    You are setting the connection only to myCommand(0), what about the other 7 ?

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Im Confused With This MySQL Query Issue

    1) There is nothing right about this query...
    Code:
    sqlquery = "select * from userlist Where username='" & My.Settings.unam & "'" & "Insert (name, site, message, about, youtube, myspace, email) VALUES( ?name, ?site, ?message, ?about, ?youtube, ?myspace, ?email)"
    You might want to try displaying it in a messagebox or something so you can see the fully formed query, then you might understand why it is syntactically wrong.

    2) In addition to what CVM noted, you never set the command text of myCommand(0) either.

    In the furure, when you do get errors, it will go a LONG way to helping solve your problems if you post WHAT the error message is.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Location
    Scotland/Japan
    Posts
    164

    Re: Im Confused With This MySQL Query Issue

    ok well the first bit of code

    vb Code:
    1. Sub useringo()
    2.         Dim conn As MySqlConnection
    3.  
    4.         'connect to DB
    5.         conn = New MySqlConnection()
    6.         conn.ConnectionString = "server=localhost; user id=user; password=pess; database=login"
    7.         'see if connection failed.
    8.         Try
    9.             conn.Open()
    10.         Catch myerror As MySqlException
    11.             MessageBox.Show("Error Connecting to Database: " & myerror.Message)
    12.         End Try
    13.         Dim myAdapter As New MySqlDataAdapter
    14.  
    15.         Dim sqlquery As String
    16.         sqlquery = "select * from userlist Where username='" & My.Settings.unam & "'" & "Insert (name, site, message, about, youtube, myspace, email) VALUES( ?name, ?site, ?message, ?about, ?youtube, ?myspace, ?email)"
    17.         Dim myCommand As New MySqlCommand()
    18.         myCommand.Connection = conn
    19.         myCommand.CommandText = sqlquery
    20.         myCommand.Parameters.AddWithValue("?name", Form4.TextBox1.Text)
    21.         myCommand.Parameters.AddWithValue("?site", Form4.TextBox3.Text)
    22.         myCommand.Parameters.AddWithValue("?message", Form4.TextBox4.Text)
    23.         myCommand.Parameters.AddWithValue("?about", Form4.TextBox5.Text)
    24.         myCommand.Parameters.AddWithValue("?youtube", Form4.TextBox6.Text)
    25.         myCommand.Parameters.AddWithValue("?myspace", Form4.TextBox7.Text)
    26.         myCommand.Parameters.AddWithValue("?email", Form4.TextBox8.Text)
    27.      
    28.         'start query
    29.         myCommand.ExecuteScalar()
    30.         conn.Close()
    31.     End Sub

    gives this error

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Insert (name, site, message, about, youtube, myspace, email) VALUES( 'hack', 'si' at line 1

    then the next code

    vb Code:
    1. Sub getsinfo()
    2.         Dim conn As MySqlConnection
    3.  
    4.         'connect to DB
    5.         conn = New MySqlConnection()
    6.         conn.ConnectionString = "server=localhost; user id=username; password=password; database=login"
    7.         'see if connection failed.
    8.         Try
    9.             conn.Open()
    10.         Catch myerror As MySqlException
    11.             MessageBox.Show("Error Connecting to Database: " & myerror.Message)
    12.         End Try
    13.         Dim myAdapter(8) As MySqlDataAdapter
    14.  
    15.         Dim sqlquery As String
    16.         Dim sqlquery2 As String
    17.         Dim sqlquery3 As String
    18.         Dim sqlquery4 As String
    19.         Dim sqlquery5 As String
    20.         Dim sqlquery6 As String
    21.         Dim sqlquery7 As String
    22.  
    23.         sqlquery = "SELECT name FROM userlist Where Username='" & My.Settings.unam & "'"
    24.         sqlquery2 = "SELECT site FROM userlist Where Username='" & My.Settings.unam & "'"
    25.         sqlquery3 = "SELECT message FROM userlist Where Username='" & My.Settings.unam & "'"
    26.         sqlquery4 = "SELECT about FROM userlist Where Username='" & My.Settings.unam & "'"
    27.         sqlquery5 = "SELECT youtube FROM userlist Where Username='" & My.Settings.unam & "'"
    28.         sqlquery6 = "SELECT myspace FROM userlist Where Username='" & My.Settings.unam & "'"
    29.         sqlquery7 = "SELECT email FROM userlist Where Username='" & My.Settings.unam & "'"
    30.        
    31.         Dim myCommand(8) As MySqlCommand
    32.  
    33.         myCommand(0).Connection = conn
    34.         myCommand(1).CommandText = sqlquery
    35.         myCommand(2).CommandText = sqlquery2
    36.         myCommand(3).CommandText = sqlquery3
    37.         myCommand(4).CommandText = sqlquery4
    38.         myCommand(5).CommandText = sqlquery5
    39.         myCommand(6).CommandText = sqlquery6
    40.         myCommand(7).CommandText = sqlquery7
    41.         'start query
    42.         myAdapter(1).SelectCommand = myCommand(0)
    43.         myAdapter(2).SelectCommand = myCommand(1)
    44.         myAdapter(3).SelectCommand = myCommand(2)
    45.         myAdapter(4).SelectCommand = myCommand(3)
    46.         myAdapter(5).SelectCommand = myCommand(4)
    47.         myAdapter(6).SelectCommand = myCommand(5)
    48.         myAdapter(7).SelectCommand = myCommand(6)
    49.         myAdapter(8).SelectCommand = myCommand(7)
    50.         Dim mydata(6) As String
    51.         mydata(0) = myCommand(1).ExecuteScalar
    52.         mydata(1) = myCommand(2).ExecuteScalar
    53.         mydata(2) = myCommand(3).ExecuteScalar
    54.         mydata(3) = myCommand(4).ExecuteScalar
    55.         mydata(4) = myCommand(5).ExecuteScalar
    56.         mydata(5) = myCommand(6).ExecuteScalar
    57.         mydata(6) = myCommand(7).ExecuteScalar
    58.  
    59.         profile.Label1.Text = mydata(0)
    60.         profile.LinkLabel1.Text = mydata(1)
    61.         profile.Label2.Text = mydata(3)
    62.         profile.TextBox1.Text = mydata(4)
    63.         profile.LinkLabel2.Text = mydata(5)
    64.         profile.LinkLabel3.Text = mydata(6)
    65.         profile.Label3.Text = mydata(7)
    66.     End Sub

    gives me on this line

    vb Code:
    1. myCommand(0).Connection = conn

    Object reference not set to an instance of an object.
    Between the world we see and the things we fear theres a realm of possibility

    D.L.H. Ind.

    We do what we do in out life to make Yours Simpler

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Im Confused With This MySQL Query Issue

    regarding your first command..... again... PRINT OUT or DISPLAY your SQL before you try to run it.... MAKE SURE it is correct. I can already tell you that it is NOT. HINT: You have two statements, that have nothing to do with each other... either SELECT or INSERT, not both (at least not the way you have it)

    Your second code - before you can use an object you first have to create it. When you created your array, what you get is essentially a carton of eggs... only it's empty. Before you can use any of the eggs, they have to be created.

    Code:
    Dim carton(12) as egg
    
    egg(0).shell = broken 'this will generat an error... the egg doesn't exist yet
    
    ...
    
    Dim carton2(12) as egg
    carton(0) = new egg   'Here we've created a new egg
    carton(0).shell = broken 'NOW we can use it
    Neat thing about .NET, you can create your carton and fill it at the same time:
    Code:
    Dim carton(12) as egg = {new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg]
    
    carton(0).shell = broken 'woot! this should work
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Location
    Scotland/Japan
    Posts
    164

    Re: Im Confused With This MySQL Query Issue

    vb Code:
    1. Sub getsinfo()
    2.         Dim conn As [CODE]if i put new here it does nothing[/CODE] MySqlConnection
    3.  
    4.         'connect to DB
    5.         conn = New MySqlConnection()
    6.         conn.ConnectionString = "server=localhost; user id=username; password=password; database=login"
    7.         'see if connection failed.
    8.         Try
    9.             conn.Open()
    10.         Catch myerror As MySqlException
    11.             MessageBox.Show("Error Connecting to Database: " & myerror.Message)
    12.         End Try
    13.         Dim myAdapter(8) As MySqlDataAdapter
    14.  
    15.         Dim sqlquery As String
    16.         Dim sqlquery2 As String
    17.         Dim sqlquery3 As String
    18.         Dim sqlquery4 As String
    19.         Dim sqlquery5 As String
    20.         Dim sqlquery6 As String
    21.         Dim sqlquery7 As String
    22.  
    23.         sqlquery = "SELECT name FROM userlist Where Username='" & My.Settings.unam & "'"
    24.         sqlquery2 = "SELECT site FROM userlist Where Username='" & My.Settings.unam & "'"
    25.         sqlquery3 = "SELECT message FROM userlist Where Username='" & My.Settings.unam & "'"
    26.         sqlquery4 = "SELECT about FROM userlist Where Username='" & My.Settings.unam & "'"
    27.         sqlquery5 = "SELECT youtube FROM userlist Where Username='" & My.Settings.unam & "'"
    28.         sqlquery6 = "SELECT myspace FROM userlist Where Username='" & My.Settings.unam & "'"
    29.         sqlquery7 = "SELECT email FROM userlist Where Username='" & My.Settings.unam & "'"
    30.        
    31.       [CODE]  Dim myCommand(8) As New MySqlCommand <<< this line errors saying that Arrays Cannot Be Declared With NEW[/CODE]
    32.  
    33.         myCommand(0).Connection = [CODE] if i put new here it says conn is not defined[/CODE] conn
    34.         myCommand(1).CommandText = sqlquery
    35.         myCommand(2).CommandText = sqlquery2
    36.         myCommand(3).CommandText = sqlquery3
    37.         myCommand(4).CommandText = sqlquery4
    38.         myCommand(5).CommandText = sqlquery5
    39.         myCommand(6).CommandText = sqlquery6
    40.         myCommand(7).CommandText = sqlquery7
    41.         'start query
    42.         myAdapter(1).SelectCommand = myCommand(0)
    43.         myAdapter(2).SelectCommand = myCommand(1)
    44.         myAdapter(3).SelectCommand = myCommand(2)
    45.         myAdapter(4).SelectCommand = myCommand(3)
    46.         myAdapter(5).SelectCommand = myCommand(4)
    47.         myAdapter(6).SelectCommand = myCommand(5)
    48.         myAdapter(7).SelectCommand = myCommand(6)
    49.         myAdapter(8).SelectCommand = myCommand(7)
    50.         Dim mydata(6) As String
    51.         mydata(0) = myCommand(1).ExecuteScalar
    52.         mydata(1) = myCommand(2).ExecuteScalar
    53.         mydata(2) = myCommand(3).ExecuteScalar
    54.         mydata(3) = myCommand(4).ExecuteScalar
    55.         mydata(4) = myCommand(5).ExecuteScalar
    56.         mydata(5) = myCommand(6).ExecuteScalar
    57.         mydata(6) = myCommand(7).ExecuteScalar
    58.  
    59.         profile.Label1.Text = mydata(0)
    60.         profile.LinkLabel1.Text = mydata(1)
    61.         profile.Label2.Text = mydata(3)
    62.         profile.TextBox1.Text = mydata(4)
    63.         profile.LinkLabel2.Text = mydata(5)
    64.         profile.LinkLabel3.Text = mydata(6)
    65.         profile.Label3.Text = mydata(7)
    66.     End Sub

    sooo i dont know what the heck is going on there and yes i know that my other command is wrong i just done know mysql so i dont know ware im going wrong yes i know that its written but but would it work better like this

    vb Code:
    1. sqlquery = "Insert Into userlist Where username='" & My.Settings.unam & "'" & "(name, site, message, about, youtube, myspace, email) VALUES( ?name, ?site, ?message, ?about, ?youtube, ?myspace, ?email)"

    sorry if i am ingoreing the ovius i just dont know this stuff very well yet
    Between the world we see and the things we fear theres a realm of possibility

    D.L.H. Ind.

    We do what we do in out life to make Yours Simpler

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Im Confused With This MySQL Query Issue

    incorrect:
    Code:
    sqlquery = "Insert Into userlist Where username='" & My.Settings.unam & "'" & "(name, site, message, about, youtube, myspace, email) VALUES( ?name, ?site, ?message, ?about, ?youtube, ?myspace, ?email)"
    correct:
    Code:
    sqlquery = "Insert Into userlist (name, site, message, about, youtube, myspace, email) VALUES( ?name, ?site, ?message, ?about, ?youtube, ?myspace, ?email)"

    You didn't follow my egg carton example:
    Code:
    Dim carton(12) as egg = {new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg}
    So... following that example...
    Code:
    Dim myCommand(8) As MySqlCommand = {new MySqlCommand, new MySqlCommand, new MySqlCommand, new MySqlCommand, new MySqlCommand, new MySqlCommand, new MySqlCommand, new MySqlCommand}
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width