rosebrit3
rosebrit3

Reputation: 523

Connection String C#

public string ConnectionString = string.Empty;

In the line above, if the connection string is assigned as string.empty, then how will the connection string get its value? I dont understand what this means exactly.

The code am reading through contains the following after the above statement:

  public DataSet GetData(SqlCommand cmd)
{
    SqlConnection conn = new SqlConnection(this.ConnectionString);
    DataSet ds = new DataSet();
    try
    {
        cmd.Connection = conn;
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        conn.Open();
        ad.Fill(ds);
        cmd.Parameters.Clear();

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        cmd.Parameters.Clear();
        conn.Close();
    }
    return ds;
}

So, here where does the connection string gets it value from

Upvotes: 0

Views: 19521

Answers (2)

krystan honour
krystan honour

Reputation: 6803

The answer to your question is that if your class has that ConnectionString as a property then the SqlConnection object here cannot connect to anything as its had the value String.Empty passed to it.

It doesn't magically work out the value its just never going to connect to anything, as you do not have a valid connection string. Does this code actually connect? if it does it can't be using String.Empty like you are describing.

To clarify I would expect the connection string to come from a settings object of some sort, either from a .config file or other such mechanism.

In an attempt to help I am guessing you are using ASP.net as below you talk about a post variable. These application types tend to read from a web.config file like below.

Then your connection string variable would be initialized by a piece of code that looked something like this, if there are multiple keys in the connectionString elements (noted by the add element) then you access via the indexer as shown.

ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["ApplicationServices"];
string connectionString = connectionStringSettings.ConnectionString;

config code follows:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="false" targetFramework="4.0" />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

Upvotes: 4

Jmyster
Jmyster

Reputation: 995

Right click on this.ConnectionString and find all refferences. I'm sure it is being set somewhere in your code befor it gets to public DataSet GetData(SqlCommand cmd)

Upvotes: 2

Related Questions