Wrox Programmer Forums

Need to download code?

View our list of code downloads.

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
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 29th, 2019, 06:39 PM
Authorized User
Points: 189, Level: 3
Points: 189, Level: 3 Points: 189, Level: 3 Points: 189, Level: 3
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2005
Location: Brandon, MS, USA.
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);
conn.Open();
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);
Console.WriteLine();
}//
}//
}//
================================================== =======
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
Reply With Quote
  #2 (permalink)  
Old April 30th, 2019, 06:45 AM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
cullancrothers (April 30th, 2019)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:21 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.