Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 29th, 2019, 06:39 PM
Authorized User
Join Date: Jun 2005
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default Help with Sqlconnection manual code for book database

Dear Imar:

I am trying to write a test manual database connection in C# to the PlanetWrox database Genre table. I have some code that gets an error. The code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Data;
using System.Data.SqlClient;

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

protected void Button1_Click(object sender, EventArgs e)
SqlDataReader rdr = null;
SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\BegASP NET\\Site\\App_Data\\PlanetWrox.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from Genre", conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
// get the results of each column
int Id = (int)rdr["Id"];
string Name = (string)rdr["Name"];
int SortOrder = (int)rdr["SortOrder"];
Console.WriteLine(Id + " " + Name + " " + SortOrder);
================================================== =======
The error is directed at the conn.Open(); line, which probably means that my manual connection string is incorrectly written. Could you please tell me how to make that code work? I am trying to write the manual code for the connection into the code behind file and make it work.

Thanks; Cullan
Cullan crothers
Old April 30th, 2019, 06:45 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Looks like you need to also escape the \ in the SQL Server instance name.

Note: for generic .NET, not Beginning ASP.NET 4.5.1 related questions, you're better off finding a different forum category such as maybe this one: https://p2p.wrox.com/general-net-122/ or another C# category.


Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
cullancrothers (April 30th, 2019)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 30: Report Custom Code - SqlConnection - Unable to Open Database fbuxo BOOK: Professional Visual Studio 2010 0 March 28th, 2013 11:49 AM
PTOpersonalization database does not exist in the book code MattHoff BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 August 18th, 2010 08:16 AM
automated code vs. manual convert21 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 October 11th, 2009 04:25 AM
Gridview manual databinding through code thas123 ASP.NET 2.0 Basics 3 December 12th, 2006 01:18 PM
Source code for Beginning Java Database book NP@--@NG Java Databases 0 April 8th, 2004 09:34 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.