Click here to Skip to main content
15,925,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DatabaseClass dqwb = new DatabaseClass();
            dqwb.dbConnection("(local)", "General", "DWSS123-PC", "dwss123");
         

          
           
            //SqlConnection myConnection1 = new SqlConnection("Server=(local);Database=General;Trusted_Connection=True;");
            //myConnection1.Open();
           
            //SqlCommand com;
            string str;
            
            try
            {
                //com = new SqlCommand();


                SqlCommand com = new SqlCommand("select YearLabel from dbo.CompanyYear");
                dqwb.openConnection();
                //com.CommandText = "select YearLabel from dbo.CompanyYear";

                SqlDataAdapter sda = new SqlDataAdapter(com);
                SqlDataReader reader = com.ExecuteReader();
                DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
}








 public void dbConnection(string ProjectName112, string databasenameName112, string user12,string pass12)
        {
            string strProject = ProjectName112; //Enter your SQL server instance name
            string strDatabase = databasenameName112; //Enter your database name
            string strUserID = user12; // Enter your SQL Server User Name
            string strPassword = pass12; // Enter your SQL Server Password



            //string strProject = a; //Enter your SQL server instance name
            //string strDatabase = b; //Enter your database name
            //string strUserID = c; // Enter your SQL Server User Name
            //string strPassword = d; // Enter your SQL Server Password

            string strconn = "data source=" + strProject + ";database=" + strDatabase + ";Persist Security Info=True;User ID=" + strUserID + ";Password=" + strPassword + ";";
           
            conn = new SqlConnection(strconn);

           
        }
Posted
Updated 3-Mar-15 0:24am
v3
Comments
CHill60 3-Mar-15 6:17am    
Is that your own DatabaseClass? If so then share the code in dbConnection
[no name] 3-Mar-15 6:18am    
yes
CHill60 3-Mar-15 6:54am    
If you put a break point after conn is assigned in dbConnection and hover over conn is there a value or is it null?
[no name] 3-Mar-15 6:58am    
null value

1 solution

Try like this-
SqlCommand com = new SqlCommand("select YearLabel from dbo.CompanyYear",myConnection1);
 
Share this answer
 
Comments
[no name] 3-Mar-15 6:20am    
cannot convert from 'void' to 'System.Data.SqlClient.SqlConnection
[no name] 3-Mar-15 6:20am    
The best overloaded method match for 'System.Data.SqlClient.SqlCommand.SqlCommand(string, System.Data.SqlClient.SqlConnection)' has some invalid arguments
Rajat_RJT 3-Mar-15 6:38am    
Did you uncomment these lines-
//SqlConnection myConnection1 = new SqlConnection("Server=(local);Database=General;Trusted_Connection=True;");
//myConnection1.Open();
make sure your connection string is correct
and if issue still exists, try restarting your VS.
[no name] 3-Mar-15 6:41am    
but sir, i want use class connection. that why i comment this.
this connection string correct i know.
Rajat_RJT 4-Mar-15 6:06am    
Once check by uncomment the above lines, and IF that works fine, then there is some problem with your connection class.

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