How to fix web connection failed to load data from my database - mysql

Im trying to create a simple database load connection through web page but it always give me an error: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
This is my web.config, using MySQL, MySQL server 2005
<connectionStrings>
<add name="MySQLConnection" connectionString="database=peronrecords;server=localhost;uid=root;pwd=root"/>
</connectionStrings>
And this is my vb code using gridview:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnection").ToString())
' Create a DataSet object.
Dim da As SqlDataAdapter
Dim dt As New DataTable
da = New SqlDataAdapter("SELECT id, name FROM student", conn)
da.Fill(dt)
' Open the connection
conn.Open()
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Sub

If you are connecting to MySql then you need to use the appropriate classes.
The SqlConnection and SqlDataAdapter are the classes required to connect to Microsoft Sql Server, for MySql you need the MySqlConnection and MySqlDataAdapter.
These classes could be found if you download and install the MySql NET Connector, then add a reference to MySql.Data.dll and add an Imports statement for MySql.Data.MySqlClient to the top of your code file.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Using conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnection").ToString())
' Create a DataSet object'
Dim da As MySqlDataAdapter
Dim dt As New DataTable
da = New MySqlDataAdapter("SELECT id, name FROM student", conn)
da.Fill(dt)
' Not needed. The DataAdapter opens the connection by itself if it is not opened
' conn.Open()
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Sub

Related

What is the ConnectionString to connect MySQL db with VB.NET

I am working on to connect my App form VB.Net with MySQL table database with this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str As String = "data source=localhost;Initial Catalog=tabledb;user id=root;password=password1234"
Dim con As New SqlConnection(str)
con.Open()
Dim com As String = "Select * from springdata where rfid_tag='" & Rfid_tagTextBox.Text & "'"
Dim cm As New SqlCommand(com, con)
Dim rd As SqlDataReader = cm.ExecuteReader()
If rd.Read() = True Then
MessageBox.Show("Valid username and password")
Else
MessageBox.Show("Invalid username and password", caption:="login")
End If
End Sub
But when I run the app gave me this error:
Additional information: A network-related or instance-specific error
occurred while establishing a connection to SQL Server. The server
was not found or was not accessible. Verify that the instance name
is correct and that SQL Server is configured to allow remote
connections. (provider: Named Pipes Provider, error: 40 - Could not
open a connection to SQL Server)
What is the correct ConnectionString to use with MySQL 5.7.
The connection string below is an SQL Server connection string:
Dim str As String = "data source=localhost;Initial Catalog=tabledb;user id=root;password=password1234"
According to MySQL Connector .NET connection string, you should provide server name, port number (if using different port), database name and credentials like this:
Dim str As String = "Server=localhost;Port=3306;Database=tabledb;Uid=root;Pwd=password1234"
Also you should use MySqlConnection, MySqlCommand & MySqlDataReader instances with parameters instead of value concatenation inside query string (ensure that reference to MySql.Data.dll added first):
Using con As New MySqlConnection(str)
con.Open()
Dim com As String = "Select * from springdata where rfid_tag=#tag"
Using cm As New MySqlCommand(com, con)
cm.Parameters.Add("#tag", MySqlDbType.VarChar).Value = Rfid_tagTextBox.Text
Dim rd As MySqlDataReader = cm.ExecuteReader()
' Check if any rows exist
If rd.HasRows = True Then
' do something
Else
' do something else
End If
End Using
End Using

Connection to MySQL issue

I would like to make a connection between my program in VB.NET and my sql database. The code is like this:
Public Function connecter()
Dim Connexion As String = "Server=197.28.178.33;Database=test;Uid=userid;Pwd=xxxxxxxxxpassxxxx;"
Dim conn As MySqlConnection = New MySqlConnection
conn.ConnectionString = Connexion
conn.Open()
Return conn
End Function
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lecteur As MySqlDataReader
Dim Requete As String
connecter.Close()
connecter()
Requete = "select * from article"
Dim Commande As New MySqlCommand(Requete, connecter)
lecteur = Commande.ExecuteReader
Do While lecteur.Read
ComboBox1.Items.Add(lecteur.GetString("description"))
Loop
connecter.Close()
End Sub
but this error appears:
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException'
occurred in MySql.Data.dll
Additional information: Unable to connect to any of the specified
MySQL hosts.
what is the cause of this error?
I don't understand what is the problem and how I can verify if I communicate with the server? and How I'm sure that the server is running

Code behide VB.connection string?

I am currently developing a webpage using ASP.NET with VB as back end. But I am facing a problem. I don't know how to make a database connection to connect with MYSQL WORKBENCH. I just wrote the connection but it just code for SQL server.
This is my sample code for connection but it for sql server. I just want code for mySQL workbench.
Imports System.Data
Imports System.Data.SqlClient
Partial Class Request
Inherits System.Web.UI.Page
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
txt1.Focus()
txt2.Focus()
txt3.Focus()
txt4.Focus()
txt5.Focus()
txt6.Focus()
txt7.Focus()
ddl1.Focus()
ddl2.Focus()
ddl3.Focus()
ddl4.Focus()
End Sub
Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
con.ConnectionString = "Server=localhost;Database=test;Uid=root;Pwd=1234;"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO cr_record (Emplid, Nama, date, DeptDesc, email, Change, reasonchange,problem,priority,reasondescription,systemrequest) VALUES (#Emplid, #Nama, #date, #DeptDesc, #email, #Change, #reasonchange,#problem,#priority,#reasondescription,#systemrequest)"
cmd.Parameters.AddWithValue("#Emplid", ddl1.Text)
cmd.Parameters.AddWithValue("#Nama", TextBox1.Text)
cmd.Parameters.AddWithValue("#date", txt5.Text)
cmd.Parameters.AddWithValue("#DeptDesc", txt2.Text)
cmd.Parameters.AddWithValue("#email", txt4.Text)
cmd.Parameters.AddWithValue("#Change", ddl2.Text)
cmd.Parameters.AddWithValue("#reasonchange", txt6.Text)
cmd.Parameters.AddWithValue("#problem", ddl3.Text)
cmd.Parameters.AddWithValue("#priority", rbl1.Text)
cmd.Parameters.AddWithValue("#reasondescription", txt7.Text)
cmd.Parameters.AddWithValue("#systemrequest", ddl4.Text)
cmd.ExecuteNonQuery()
con.Close()
MsgBox("Added Successfully")
End Sub
End Class
You will need to download and reference the MySQL .NET Connector http://dev.mysql.com/downloads/connector/net/. You will then use the System.Data.MySqlClient namespace and replace your SqlConnection and SqlCommand with MySqlConnection and MySqlCommand

I'm trying to connect to mysql with vb 2010 express, Access denied

i have already read a ton of posts about connecting vb 2010 express to Mysql in Wamp, but none solved my problem :P.
The thing is that i have a local Mysql server running, to which i do connect with no problems in PHP and MySQL Workbench, but when i tried to connect with vb 2010 express i found out i had to Import the connector and then build the code accordingly.
I think my code is ok, but still getting the message "WindowsApplication1.vshost.exe Error: 0 : Access denied for user 'root'#'localhost' (using password: YES)"
Here's the code:
Private Sub CheckPayments()
Try
myPassword = ""
myConnstr = "Server=localhost;Database=kiosk;Uid=root;Pwd=myPassword"
myQuery = "select * from products where deleted = 0"
Dim myConn As New MySqlConnection(myConnstr)
Dim myDa As New MySqlDataAdapter(myQuery, myConn)
If myDa.Fill(myDs) Then ' 1 significa que a ligação foi efetuada com sucesso
mysqldump.DataSource = myDs.Tables(0)
End If
myConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Stuck in project with this error, need help quick !!
Thanks in advance for any help you can give me :)
BR, Carlos Mendes
hope this helps out someone who needs to connect VB2010 Express to MySQL.
This code displays MySQL data in a DataGridView object. Connections problem solved :).
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=<your db>;User ID=<your user>;Password=<your password>;")
Dim sql As MySqlCommand = New MySqlCommand("SELECT * FROM <your table>", con)
Dim ds As DataSet = New DataSet()
Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
con.Open()
DataAdapter1.SelectCommand = sql
DataAdapter1.Fill(ds, "<DataGridView table name>")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "<DataGridView table name>"
con.Close()
End Sub
You should Import Connecter, is better and good way with out any trouble!
mysql-connector-net-6.4.5-noinstall
Then go to
Project -> Add Reference -> TAB-Browse
Browse where you save "mysql-connector-net-6.2.2"
Select only mysql.data.dll
Then use your Connection command to connect WAMP MYSQL!
Thats It..

How to connect visual basic 2008 to sql server 2008?

Can anyone please guide me on how to connect Visual Basic 2008 to an SQL server 2008 database? I'm confused on what is the best practice when it comes to this.
There are many many ways. However this is the one of the simplest just using the .net libraries.
you connect to the DB using the server, db and user names and password
you issue a sql command
you collect the results in a sqldatareader
you iterate over the results in the reader
you clean up the the resources.
From: How to ADO.NET SqlDataReader
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim sql As String
' Use this first connection string if using windows auth
' connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True"
connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
sql = "Your SQL Statement Here , like Select * from product"
sqlCnn = New SqlConnection(connectionString)
Try
sqlCnn.Open()
sqlCmd = New SqlCommand(sql, sqlCnn)
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
While sqlReader.Read()
MsgBox(sqlReader.Item(0) & " - " & sqlReader.Item(1) & " - " & sqlReader.Item(2))
End While
sqlReader.Close()
sqlCmd.Dispose()
sqlCnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class
From MSDN : Connecting to Microsoft SQL Server 2008 from Microsoft Visual Studio 2005 and 2008