Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i am returning the connectionstring in string format i am getting an error saying Cannot Convert from 'System.Data.SqlClient.SqlConnection' to 'String'

Can anyone explain me

What I have tried:

public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
    if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");

    // Create & open a SqlConnection, and dispose of it after we are done
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        // Call the overload that takes a connection in place of the connection string
        return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
    }
}
Posted
Updated 3-Mar-18 2:46am
Comments
Member 8583441 3-Mar-18 8:05am    
The error is at return ExecuteNonQuery(connection, commandType, commandText, commandParameters); this line at connection

Why do you call the same method recursively? You're inside ExecuteNonQuery method and calling it again.

Did you intend to call the ExecoteNonQuery method of the command

C#
...
connection.Open();
SqlCommand command = new SqlCommand(commandText, connection);
command.Connection.Open();
command.ExecuteNonQuery();
...
 
Share this answer
 
Comments
Member 8583441 3-Mar-18 10:22am    
My problem has not been solved with this solution
Wendelius 3-Mar-18 11:02am    
Perhaps you could give more information about the problem. Are you still facing an error or having some other issue?
Wendelius 3-Mar-18 11:08am    
You could also go through the following article. It could clarify the way to use Sql objects: Properly executing database operations[^]
Member 8583441 3-Mar-18 10:23am    
Sorry Hi Wendelius sir..... I doesn't mention it before saying Hi in the previous comment
I typed this off the top of my head, so it might need some minor tweaking, but this is a more correct way to do this.

C#
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
    int result = 0;
    if (string.IsNullOrEmpty(connectionString)
    {
        throw new ArgumentNullException("connectionString");
    }

    try
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using SqlCommand cmd = new SqlCommand(commandText, conn))
            { 
                cmd.CommandType = commandType;
                cmd.Parameters.AddRange(commandParameters);
                result = cmd.ExecuteNonQuery();
            }
        }
    }
    catch (Exception ex)
    {
       // handle the exception here
    }
    return result;
}
 
Share this answer
 
v3
Comments
Member 8583441 3-Mar-18 10:21am    
Hi John, I've tried this code but getting error at cmd.Parameters saying 'Property or Indexer "SqlCommand.Parameters" cannot be assigned to -- it is read-only
#realJSOP 3-Mar-18 11:41am    
I said it might need tweaking. I edited my answer.
Member 8583441 4-Mar-18 5:27am    
Thanks for the solution John

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