Web.config creates ConnectionString error in class?

J

JB

Hello

I am writing an ASP.NET web application and I am using the Web.config file
to provide the connection string info but when code runs and gets to the
line where
the DataAdapter is to fill the dataset the error states:

"Fill:SelectCommand.Connection property has not been initialized" (THIS IS
OCCURS ON THE LINE THAT SAYS: da.fill(carDs, "Cars");

The Web.config file is set up properly and it class refers to it, as
shown below:

THIS PART IS THE FORM:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnFillDataClick(object sender, EventArgs e)
{
cardata allcars = new cardata();
allcars.getcar();
carsGridView.DataSource = allcars;

}

}

-------------------------------------------------------------------------
THIS IS WHERE I USE THE CONNECTIONSTRING ERROR OCCURS:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for cardata
/// </summary>
public class cardata
{
public cardata()
{
//
// TODO: Add constructor logic here
//
}
public void getcar()
{
string provide = ConfigurationManager.AppSettings["provider"];

string carConnect =
ConfigurationManager.ConnectionStrings["cartable"].ConnectionString;
SqlConnection cn = new SqlConnection(carConnect);

SqlCommand cmd = new SqlCommand("select * from Cars, cn ");
SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;
cn.Open();

DataSet carDs = new DataSet();
da.Fill(carDs, "Cars"); --THIS IS THE ERROR LINE
cn.Close();

}
}
----------------------------------------------------------------------
THIS IS HOW THE APPSETTINGS AND CONNECTIONSTRING IN WEB.CONFIG LOOKS
<appSettings>

<add key="provider" value="System.Data.SqlClient" />

<!--<add key="cartable" value ="Data
Source=WHPCOMPAQCOP19;Integrated Security=SSPI; Initial Catalog=cardb" /> -->

<!-- <add key="ConnectionInfo"
value="server=(local);database=Northwind;Integrated Security=SSPI" /> -->

</appSettings>

<!-- <appSettings/> -->


<connectionStrings>

<!-- <add name="cardbConnectionString" connectionString="Data
Source=WHPCOMPAQCOP19;Initial Catalog=cardb;Integrated Security=True"
providerName="System.Data.SqlClient"/> -->

<add name="cartable" connectionString="Data
Source=WHPCOMPAQCOP19;Integrated Security=SSPI; Initial Catalog=cardb" />

</connectionStrings>
--------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Car
/// </summary>
public class Car
{
//
// TODO: Add constructor logic here
//
public string carid { get; set; }
public string carPetName { get; set; }
public string carMake { get; set; }
public string carColor { get; set; }

public Car(string Carid, string make, string color, string petName)
{
carid = Carid;
carMake = make;
carColor = color;
carPetName = petName;
}
}
 
J

Jeff Dillon

JB said:
Hello

I am writing an ASP.NET web application and I am using the Web.config
file
to provide the connection string info but when code runs and gets to the
line where
the DataAdapter is to fill the dataset the error states:

"Fill:SelectCommand.Connection property has not been initialized" (THIS IS
OCCURS ON THE LINE THAT SAYS: da.fill(carDs, "Cars");

The Web.config file is set up properly and it class refers to it, as
shown below:

THIS PART IS THE FORM:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnFillDataClick(object sender, EventArgs e)
{
cardata allcars = new cardata();
allcars.getcar();
carsGridView.DataSource = allcars;

}

}

-------------------------------------------------------------------------
THIS IS WHERE I USE THE CONNECTIONSTRING ERROR OCCURS:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for cardata
/// </summary>
public class cardata
{
public cardata()
{
//
// TODO: Add constructor logic here
//
}
public void getcar()
{
string provide = ConfigurationManager.AppSettings["provider"];

string carConnect =
ConfigurationManager.ConnectionStrings["cartable"].ConnectionString;
SqlConnection cn = new SqlConnection(carConnect);

SqlCommand cmd = new SqlCommand("select * from Cars, cn ");
SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;
cn.Open();

DataSet carDs = new DataSet();
da.Fill(carDs, "Cars"); --THIS IS THE ERROR LINE
cn.Close();

}
}
----------------------------------------------------------------------
THIS IS HOW THE APPSETTINGS AND CONNECTIONSTRING IN WEB.CONFIG LOOKS
<appSettings>

<add key="provider" value="System.Data.SqlClient" />

<!--<add key="cartable" value ="Data
Source=WHPCOMPAQCOP19;Integrated Security=SSPI; Initial Catalog=cardb"
/> -->

<!-- <add key="ConnectionInfo"
value="server=(local);database=Northwind;Integrated Security=SSPI" /> -->

</appSettings>

<!-- <appSettings/> -->


<connectionStrings>

<!-- <add name="cardbConnectionString" connectionString="Data
Source=WHPCOMPAQCOP19;Initial Catalog=cardb;Integrated Security=True"
providerName="System.Data.SqlClient"/> -->

<add name="cartable" connectionString="Data
Source=WHPCOMPAQCOP19;Integrated Security=SSPI; Initial Catalog=cardb" />

</connectionStrings>
--------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Car
/// </summary>
public class Car
{
//
// TODO: Add constructor logic here
//
public string carid { get; set; }
public string carPetName { get; set; }
public string carMake { get; set; }
public string carColor { get; set; }

public Car(string Carid, string make, string color, string petName)
{
carid = Carid;
carMake = make;
carColor = color;
carPetName = petName;
}
}

Should be SqlCommand cmd = new SqlCommand("select * from Cars", cn );

and not

SqlCommand cmd = new SqlCommand("select * from Cars, cn ");
 
J

JB

Thank you Lee

However cmd. does not have an "Open()" option in intellisense in this
particular case but putting the quote after the select statement and before
the "cn" did get pass the error though.

Thank you
JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top