Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in app_code, there are two folder
1. Connection
2. Login
connection class manage multiple connection string
code is:-

C#
public class Connection
{
    string conn;
  SqlConnection con;
    public Connection( string val)
    {
        if (val == "1")
        {
            conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        }
        else if (val == "2")
            {
                conn = ConfigurationManager.ConnectionStrings["connectionString1"].ConnectionString;
            }
        
       //  TODO: Add constructor logic here
        
      con = new SqlConnection(conn);
    }

and login class code :-

C#
public class Login
{
	public Login()
	{
	}
 
    public string isLogin()
    {
       
      Connection clsConn= new Connection("2");
       SqlCommand cmd = new SqlCommand("login", clsConn );
    }
}
here i am not access clsconn in sqlcommand so anyone tell me how can i access ??? please
Posted
Updated 15-Jan-15 23:19pm
v3
Comments
CHill60 16-Jan-15 5:20am    
What do you mean by no access - what is the actual error message?
J{0}Y 16-Jan-15 6:15am    
the best overloaded method match for 'System.Data.SqlCommand.SqlCommand(string, System.Data.SqlClient.SqlClient)' has some invalid arguments

http://msdn.microsoft.com/en-us/library/877h0y3a(v=vs.110).aspx[^]
says about SqlCommand Constructor:
VB
public SqlCommand(
    string cmdText,
    SqlConnection connection
)

Therefore second constructor parameter must be an object of SqlConnection class.

But your second constructor parameter is an object of a completely different class.

This cannot work!

You must change your code like this:
C#
SqlCommand cmd = new SqlCommand("login", clsConn.con);

Make sure that con is public.
C#
public SqlConnection con;
 
Share this answer
 
v3
change your Connection class to get connection as below
C#
public class Connection
{
   
    public static SqlConnection  GetConnection( string val)
    {    string conn ="";
        if (val == "1")
        {
            conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        }
        else if (val == "2")
            {
                conn = ConfigurationManager.ConnectionStrings["connectionString1"].ConnectionString;
            }
        
      return new SqlConnection(conn);
    }
}


then you can do as below in other classes
C#
public string isLogin()
   {

      SqlConnection clsConn= Connection.GetConnection("2");
      SqlCommand cmd = new SqlCommand("login", clsConn);
   }
 
Share this answer
 
v2
Comments
J{0}Y 16-Jan-15 6:09am    
one error shown :- use of unassigned local variable 'conn'
DamithSL 16-Jan-15 7:00am    
Answer updated, please check
J{0}Y 16-Jan-15 7:13am    
thanks :)
I think you need

SqlCommand cmd = new SqlCommand("login", clsConn.con );

Since your Connection does not inherit from sqlConnection, you have to get the connection object from your Connectio class.

I would suggest you make public property Connection instead of accessing the variable directly.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 

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