Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i created connection string in a class but i am not able to use database.


public class DataContext
{
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    //SqlDataReader dr = new SqlDataReader();
    public void Openconnection()
    {
        if (con.State == ConnectionState.Closed)
        {
            try
            {
                string constring = "data source = myserveraddress;Initial Catalog=mywebsite;Persist Security Info=True;User ID=myid;Password=mypassword";
                con.ConnectionString = constring;
                con.Open();
                cmd.Connection = con;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
    }
    public void CloseConnection()
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
    }
    public DataTable getDataTable(string Query)
    {
        DataTable dt = new DataTable();
        Openconnection();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = Query;
        da.SelectCommand = cmd;
        da.Fill(dt);
        CloseConnection();
        return dt;
    }
    public int executeNonquery(string Query)
    {
        Openconnection();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = Query;
        int i = cmd.ExecuteNonQuery();
        CloseConnection();
        return i;
    }


i am deploying my website by using connction strin like this...
Posted
Updated 29-Mar-11 10:06am
v2

1 solution

Try adding "Data Source=..." in the beginning of the connection string. For future use: http://www.connectionstrings.com/sql-server-2005[^]
 
Share this answer
 
Comments
se.nishant 29-Mar-11 16:07pm    
i still have same problem..
Wendelius 29-Mar-11 16:14pm    
What is the error you get?
se.nishant 29-Mar-11 16:24pm    
Fill: SelectCommand.Connection property has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.]
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +223
System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +504
System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +324
DataContext.getDataTable(String Query) in C:\Users\nishant\word bitmaps and others\WebApplication2\WebApplication2\DataContext.cs:47
BusinessLogic.Getarticlesforindex() in C:\Users\nishant\word bitmaps and others\WebApplication2\WebApplication2\BusinessLogic.cs:218
WebApplication2._Default.Page_Load(Object sender, EventArgs e) in C:\Users\nishant\word bitmaps and others\WebApplication2\WebApplication2\index.aspx.cs:19
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3048
se.nishant 29-Mar-11 16:25pm    
this is the error
Wendelius 29-Mar-11 16:40pm    
Ok, you're also missing the assignment of the connection. Add:
...
Openconnection();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
...

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