3 Replies - 1161 Views - Last Post: 10 August 2009 - 07:32 AM Rate Topic: -----

#1 J0K3R  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 298
  • Joined: 19-December 08

ASP.NET C# Connectionstring

Post icon  Posted 09 August 2009 - 10:13 AM

Hi.

Im learning ASP.NET C# now and I am using the Express Web Developer IDE from Microsoft with Express SQL 2008.

So I created a database in my App_data folder called Database.mdf.

Im trying to create a connection string but cant find anything that works.

Can you please help me...

Here is my code for my connection.


protected bool OpenDatabaseConnection()
	{
		string dbConnectionString = "Server=.\\SQLExpress;AttachDbFilename=|DataDirectory|Database.mdf; Database=Database;Trusted_Connection=Yes;";
		// create a new database connection object
		// assign our built connection string to this object
		DBConnection = new SqlConnection(dbConnectionString);

		// do a test to see if connection will be successful
		try
		{
			DBConnection.Open();
			DBConnection.Close();
		}
		catch (SqlException)
		{
			return false;
		}

		return true;
	}




:crazy:

Is This A Good Question/Topic? 0
  • +

Replies To: ASP.NET C# Connectionstring

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: ASP.NET C# Connectionstring

Posted 09 August 2009 - 02:50 PM

Your connection string looks correct.

What error message are you receiving?
Was This Post Helpful? 0
  • +
  • -

#3 J0K3R  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 298
  • Joined: 19-December 08

Re: ASP.NET C# Connectionstring

Posted 10 August 2009 - 12:44 AM

Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 43: cmd.Parameters.AddWithValue("@pageid", pageid);
Line 44:
Line 45: DBConnection.Open();
Line 46: SqlDataReader myReader = cmd.ExecuteReader();
Line 47:
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: ASP.NET C# Connectionstring

Posted 10 August 2009 - 07:32 AM

Perhaps you should post the code relevant to the error message.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1