Question Locked

Problem with SqlConnection

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello, I have a problem with MS SQL Server SqlConnection and SqlHelper class, but I don't have any compilation errors:

Here is a method in my class:

public SqlDataReader showCategories()
{
//string dbLink = ConfigurationManager.AppSettings["cnstr"];
SqlDataReader dtrCategories;
try
{
dtrCategories = SqlHelper.ExecuteReader(ConfigurationManager.AppSettings["cnstr"], CommandType.StoredProcedure, "spCategoriesShow");
dtrCategories.Read();

}
catch(Exception ex)
{
throw ex;

}
return dtrCategories;
}

Then I have page where I call previous method and try bind data to Repeater:

protected void Page_Load(object sender, EventArgs e)
{
Categories myCat = new Categories();
myCat.Name="Adam";
lblImie.Text = myCat.Name.ToUpper();
rptCategoriesShow.DataSource = myCat.showCategories();
rptCategoriesShow.DataBind();
/* SqlConnection dblink = new SqlConnection(ConfigurationManager.AppSettings["cntstr"]);

try
{
dblink.Open();
lblImie.Text += " Jest połączenie";
}
catch (Exception ex)
{
lblImie.Text = ex.ToString();
}
finally
{
dblink.Close();
}
*/
}

But there is something wrog: I receive error:

Server Error in '/3t2' Application.
The ConnectionString property has not been initialized.

Any suggestions

5 replies

by tony hopkinson

ConfigurationManager.AppSettings["cnstr"]

the error would suggest it's null.

I not in somesplaces in the code you have cntstr ....

by abezulski

That's it! I made spelling mistake. Thanks! I don't know how could i miss that.

by tony hopkinson

Craete a constant for the string literal some place, then at least they'l all be spelled the same though not necessarily correctly.

This discussion is closed to new replies.