Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Two Query is under blow first is main 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:

Main Query
Dim SqlConnection As String = ConfigurationManager.ConnectionStrings("RMS.My.MySettings.SoftlinksStupConnection").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") 'Change items to your database name

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 7:26am
Comments
Computechsoft 18-Sep-18 14:08pm    
This Ok but i want Vb.net
Richard Deeming 18-Sep-18 16:40pm    
You've posted this as a reply to your question. I suspect you meant to post it as a reply to Solution 1 instead.

1 solution

The error is telling you that you can't convert a String into a SqlConnection. So, you are passing a string in as the second parameter to the SqlCommand constructor. You need to pass a SqlConnection, not just the Connection String.

SqlCommand Constructor (System.Data.SqlClient) | Microsoft Docs[^]

However, the better approach is to use the using statement, something like this:
C#
using (SqlConnection con = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("SELECT ...", con)) // notice, pass the actual SqlConnection and not the string of the connection.
    {
    }
}


This sample is in C#. But the main point is to pass the proper parameter.
 
Share this answer
 
Comments
Maciej Los 18-Sep-18 16:25pm    
5ed!

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