0

So I have been trying to figure out why my login system hasn't been working. Is there a logical tutorial somewere online I can watch someone making one? or could you guys please help me figure this NOT on my line? :( I am sorry, First time working with SQL in Vb. Btw it is a distant hosted sql server.

Code:

Imports MySql.Data.MySqlClient
Imports System.Data.SqlClient

Public Class Form1
    Dim conn As MySqlConnection
    Private Sub ReconButton1_Click(sender As System.Object, e As System.EventArgs) Handles ReconButton1.Click
        Dim conn As MySqlConnection

        conn = New MySqlConnection()
        conn.ConnectionString = "server=Host; user id=User; password=Pass; database=DB"
        'see if connection failed
       Try
           conn.Open()

       Catch myerror As MySqlException
           MsgBox("Error connecting to server", MsgBoxStyle.Exclamation, "Error")
       End Try
       'sql query
        Dim myAdapter As New MySqlDataAdapter

        Dim sqlquery = "SELECT [] FROM users WHERE username = '" + TxtBox1.Text + "' AND password = '" + TxtBox2.Text + "'"
        Dim myCommand As New MySqlCommand()
        myCommand.Connection = conn
        myCommand.CommandText = sqlquery
        'start query
       myAdapter.SelectCommand = myCommand
`It highlights myAdapter.SelectCommand = myCommand`
       Dim mydata As MySqlDataReader
       mydata = myCommand.ExecuteReader()
       'see if user exist
        If mydata.HasRows = 0 Then MsgBox("Invalid Login!") Else MsgBox("Login Accepted!")
        Welcome.Show()
        Me.Close()
    End Sub
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Domanate
  • 1
  • 2
  • What is the actual problem? – ChrisBint Dec 17 '14 at 22:23
  • 2
    Also, do a search for "parameterized queries". Don't ever use code like That's a big security hole. –  Dec 17 '14 at 22:25
  • You have posted a lot of text on here @Domanate, but not once have you explained your issue or posted an actual error. – Nick.Mc Dec 17 '14 at 23:15
  • @Nick.McDermaid It will not sync with my sql server that is hosted by my site host. That is my issue :( – Domanate Dec 17 '14 at 23:30
  • Do you know that because you get an error message or because the remote local is not the same as the remote data? If the error is being caught I suggest you display more detailed error info from the error object. – Nick.Mc Dec 17 '14 at 23:50
  • http://pastebin.com/YGRVwVrA - Entire sheet of the code. It will Highlight 'myAdapter.SelectCommand = myCommand` – Domanate Dec 17 '14 at 23:54
  • You have already pasted tour code. When it highlights that line (I assume because it breaks due to an error), what error does it show? – Nick.Mc Dec 18 '14 at 00:59
  • Nothing. It just highlights it. Could I add you on my personal skype? – Domanate Dec 18 '14 at 01:42
  • my email is Administrator@AquaProjectHF.tk i will send you my username :P just pm me when ever, then you will have a visual example to go by :P – Domanate Dec 18 '14 at 01:46
  • Sorry I don't think I'm going to be able to help, I've asked three times for the actual error and you haven't been able to explain it. Before you can solve your problem you have to define it. – Nick.Mc Dec 18 '14 at 22:56

1 Answers1

0

Theres a few things I notice...

  1. Use parameters always, dont skip them.
  2. Your connection is wrong evidentially.
  3. You said trouble with SQL, but your code is using MySQL namespace, this is wrong for SQL and they are different. If its SQL then you need to use SqlConnection namespace. You decide what DBMS to use...
  4. Also your catch will still throw an exception. If you dont have a connection then you cant do another query like your doing. The purpose is to catch the exception which you do but then try a query again, that will bomb.

Now you have a few options here...

  1. Use a VPN to connect like Hamachi. This will give you a unique IP to use to connect.
  2. Make sure your server is indeed reachable. If you cant reach the pc to get the IP, you should be able to get this in your admin area for the server.

An online connection string is no different from a local one. Just replace the serverip part with the public facing IP address of your "online" server that's it. Also if it is the default instance then only ip will do for you, but if it is the user defined instance then you have to mention the ip\instance name too.

For example replace: server=Host ... To this...

 server=124.553.546.65 'Your server IP, this is only an example
Trevor
  • 7,777
  • 6
  • 31
  • 50
  • I shared only the rough template, I already added the host.... I wouldn't share that info outloud... – Domanate Dec 18 '14 at 01:44
  • Well of course not. I was explaining its all wrong, did you understand what I have explained in my answer? – Trevor Dec 18 '14 at 02:02
  • mhm I guess, The problem is sorta hard to explain. It would help more visually. Could you email me and ill forward screen shots of all that happens. I am not experienced with SQL when it comes to client share connections.[VB] I have struggled to find someone to assist me for the past 2 years. I could never find a solution. :( – Domanate Dec 18 '14 at 14:08
  • 1
    Are you indeed dealing with SQL or MySql ? – Trevor Dec 18 '14 at 14:10
  • MySQL is what I am trying to use. – Domanate Dec 18 '14 at 20:28
  • Well I hate to say, but this is completely different from SQL. You posted a question about an issue with SQL and how you cant connect. You need to redo this question explaining this as its VERY misleading. I have already asked this question before and you haven't answered yet. Please make changes to you problem/issue. – Trevor Dec 18 '14 at 23:02