Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Two Query is under blow First connection

Query 1 Run is Correct no error

Query 2 Error is

Value type string can't be converted to system.data.SqlClient.SqlConnection


What I have tried:

Connection
Dim SqlConnection As String = ConfigurationManager.ConnectionStrings("RMS.My.MySettings.StupConnection").ConnectionString
System.Configuration.ConfigurationManager.AppSettings.Get("SQLSqriptRead")


Query 1

 Dim da_Product_Items = New SqlClient.SqlDataAdapter("Select Name,Address,Phone from dbo.Script order by Name", SqlConnection)
 da_Product_Items.Fill(ds_Product_Items, "Script") 

Query 2

Dim Check_User_Name_Found As New SqlClient.SqlCommand("SELECT Max(User_Name) FROM SQLDatabase.dbo.Administrator_Account", SqlConnection)
Dim Check_UserNameFound = Check_User_Name_Found.ExecuteScalar().ToString()
Posted
Updated 18-Sep-18 20:20pm

1 solution

The error message is pretty explicit:
Value type string can't be converted to system.data.SqlClient.SqlConnection
And it means exactly what it says.
Look at your code:
Dim SqlConnection As String = ... 

Dim Check_User_Name_Found As New SqlClient.SqlCommand("SELECT ...", SqlConnection)
SqlConnextion is a String, not an SqlConnection instance.
Somewhere, you need to create an SqlConnecion instance using the connection string you read from your ConfigurationManager and open it. You can then use it to create your SqlCommand instance - but you can't just hand it a string and hope that it works, because it won't.

And a quick check says that you were told this 13 hours ago: Sql connection error[^]
The problem hasn't changed, the solution hasn't changed - you need to think about what you are doing and change your code!
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using cmd As New SqlCommand("SELECT iD, description FROM myTable", con)
		Using reader As SqlDataReader = cmd.ExecuteReader()
			While reader.Read()
				Dim id__1 As Integer = CInt(reader("iD"))
				Dim desc As String = DirectCast(reader("description"), String)
				Console.WriteLine("ID: {0}" & vbLf & "    {1}", iD, desc)
			End While
		End Using
	End Using
End Using
 
Share this answer
 
Comments
Computechsoft 19-Sep-18 2:39am    
Thanks Permalink it is help full
OriginalGriff 19-Sep-18 3:39am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900