Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9
This is the forum to discuss the Wrox book Professional ASP.NET 2.0 Special Edition by Bill Evjen, Scott Hanselman, Devin Rader, Farhan Muhammad, Srinivasa Sivakumar; ISBN: 9780470041789
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 15th, 2006, 07:10 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shysusan
Default 12-1 ConfigurationManager

I am running the following code from Chap 12

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs)

        If Not Page.IsPostBack Then
            Dim MyConnection As SqlConnection
            Dim MyCommand As SqlCommand
            Dim MyReader As SqlDataReader

            MyConnection = New SqlConnection()
            MyConnection.ConnectionString = _
       ConfigurationManager.ConnectionStrings("DSN_Northw ind").ConnectionString

            MyCommand = New SqlCommand()
            MyCommand.CommandText = "SELECT TOP 3 * FROM CUSTOMERS"
            MyCommand.CommandType = CommandType.Text
            MyCommand.Connection = MyConnection

            MyCommand.Connection.Open()
            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConne ction)

            gvCustomers.DataSource = MyReader
            gvCustomers.DataBind()

            MyCommand.Dispose()
            MyConnection.Dispose()
        End If
    End Sub
</script>

<html>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvCustomers" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

But when ever I get to the line

 MyConnection.ConnectionString = _
       ConfigurationManager.ConnectionStrings("DSN_Northw ind").ConnectionString


The code falls on its ass with the error message

Use the key word New to create an object Instance, I have been looking at this for some time and have even downloaded the code from the website and yet still it gives me the same error -HELP what am I doing wrong!:(

Shy




  #2 (permalink)  
Old October 20th, 2006, 08:26 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shysusan
Default

Come on guys help me out what am i doing wrong. This is driving me nuts!!:(

  #3 (permalink)  
Old October 20th, 2006, 08:43 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shysusan
Default

Thanks for all your help guys. I have worked it out myself:)!

  #4 (permalink)  
Old October 21st, 2006, 03:14 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

For the benefit of the archive of this forum, would you mind sharing how you fixed it?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #5 (permalink)  
Old February 11th, 2007, 02:43 PM
Authorized User
Points: 176, Level: 3
Points: 176, Level: 3 Points: 176, Level: 3 Points: 176, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2006
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OOhh Man :) the stupidity is Triumphs.
Ok. This one was Funny!
I had spend about 20 minutes, before i found this stupid problem :)
I can't believe that I missed this crappy GridView with the gvCustomers ID :)
So if you handType the code from the book, don't forget to create a simple GridView Control after the main code: Between the <form><asp:GridView ID = gvCustomers runat="server"/></form> open/closing tags

Problem number 2. the ConnectionString.
Now, I really don't understand why the authors never discuss this VERY Simple issue.
..Of course many of us will type the connection string as is presented in this Example.
But, MAAAN! As always, this connectionstring simply will not work with the SqlExpress2005 server, Why?
First: Simply because you will need to provide the unique connection string of your SQLServer Installed on your PC.
Second:Seems like you must provide a connection string before runtime.
Which means the connectionstring must appear in the webconfig before you gonna run this code. and that means that, the only way to make this work, is somehow to connect your project to the DataBase before running the code.
So where the hell you can find this Connectionstring?
This is very simply. If the connection string of this example will not work,and it probably will not work :)
You will probably receive some kind of NULLREFERENCE EXCEPTION error, that says:
I can't find your server man. So if you have this problem, do the following:

* Switch to design-mode.
* Drag the SqlDataSource Control to the white-Page(This will create a SqlDataSource Control on the screen)
* Open theSmartTagMenu of the SqlDataSource Control
* Choose the: ConfigureDataSource Link and click it.
* Then click the New Connection... Button.(Then click the Next Button)
* Notice, when the Add Connection window pops up, the (Server name: field) probably will be empty.
* What server name to provide?
First you must provide the machine key,and right after the machine key, you will provide the sqlexpress name.

Example: K-N3TI8E2ZJ61H2\SQLEXPRESS

***Notice that the Full SqlServer2005 will probably need only the machine key:
Example: K-N3TI8E2ZJ61H2

* After this, simply select your prefered DataBase from the DropDownList, and click Test Connection button, to verify your connection. Then click OK button, and follow these steps.
* Do not change the Default name in the Step[2]The default name will probably be:NorthwindConnectionString

After finishing the wizard simply click the finish button.
The above steps will connect/bind your SqlDataSource Control to the SQL2005Server.
* Ok, now switch back to the source View, and check the connection string of your SqlDataSource Control.
You will probably see something like this:
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
* Simply Copy the name after the COLON: NorthwindConnectionString
* So now instead typing:
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["DSN_Northwind"].ConnectionString;
Type this:
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

The magic behind the scene
So why I suggested you to create this SqlDataSource Control?
Simply because, by creating this control and bind it to the SqlExpress2005 Server using the wizard.
This creates an internal connection string, in the WEB.CONFIG file.
What it does, it gives you a chance to see the real connection that SqlDataSource had created for you automatically,and it's Undoubtedly connects to the database before a runtime!
So now your project will connect to the database, and you'll get a chance to discover
how your connection string is looks.No need to guess it :)!!!
In a case you already connected to your database and your connection string doesn't work, simply open the webConfig file and check the NAME of your connection string that connects to your data base. Then simply provide the same exact name to your connection string in the aspx page.

WebConfig------------------

<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=K-N3TI8E2ZJ61H2\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
So now, once you connected with the SqlServerExpress you can easily figure out what the real name of your connection string will be. it will probably be the same connection string that you provided in the Add connection wizard STEP2 remember? NorthwindConnectionString
is a default NAME!
As you see from the Image, you can easily change here the name of the connectionstring to any name you want. So, if you want to change the name, simply change it in the web.config file, and then change it to exact same name in the VB/c# code in aspx Page

Here is the code:
//---------------------------------------------------------------------------
<%@ Page Language="C#" %>
<%@ Import Namespace ="System.Data" %>
<%@ Import Namespace= "System.Data.SqlClient" %>
<%@ Import Namespace ="System.Configuration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlConnection MyConnection;
            SqlCommand MyCommand;
            SqlDataReader MyReader;

            MyConnection = new SqlConnection();
            MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

            MyCommand = new SqlCommand();
            MyCommand.CommandText = "SELECT TOP 3 * FROM CUSTOMERS";
            MyCommand.CommandType = CommandType.Text;
            MyCommand.Connection = MyConnection;

            MyCommand.Connection.Open();
            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConne ction);

            gvCustomers.DataSource = MyReader;
            gvCustomers.DataBind();


            MyCommand.Dispose();
            MyConnection.Dispose();

        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Listing12-1 The SqlConnection, SqlCommand, and SqlDataReader objects</title>
</head>
<body>
<h2>Listing12-1: The SqlConnection, SqlCommand, and SqlDataReader objects<br />
<span style ="font-family:Verdana; font-size:10pt; color:Red; font-weight:normal">Listing 12-1 shows the Connection, Command, and DataReader objects in action.<br />
It shows how to connect with the Northwind database, read the Customers table and show the results in a GridView control.</span></h2>

    <form id="form1" runat="server">
    <div>
    <asp:GridView ID ="gvCustomers" runat ="server"></asp:GridView>
        &nbsp;&nbsp;<br />
        <br />
        &nbsp;<br />
    </div>
    </form>
</body>
</html>
  #6 (permalink)  
Old February 13th, 2007, 12:42 AM
Authorized User
Points: 176, Level: 3
Points: 176, Level: 3 Points: 176, Level: 3 Points: 176, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2006
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Continuation:
Do not override the above sqlDataSource creating, because you will still need to connect your project to the DataBase somehow.
You will still need this Record(connection string) in the configuration file.
So keep in mind after typing all of the code, before creating the
ConnectionString in the aspx page:
First create the SqlDataSource control as described above(previous topic).
Then go to the Configuration file explore the connection string name, then back to the main code:
Example
aspx page.... IMPORTANT: use Square brackets [] instead circle ones()
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("Bla_bla_bl a").ConnectionString;
and provide the same name for the connection string, exactly as is appears in the webconfig file.
WebConfig file.......
<connectionStrings>
<add name="Bla_bla_bla" connectionString="Data Source=K-N3TI8E2ZJ61H2\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

**Yes after all you can DELETE the SqlDataSource Control
Because you use it only for creating the first time connection to the database. once you connected to the SqlExpress and you got the Connectionstring in your web.config, simply delete your SqlDataSource control.
 


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
can we use Configuration, ConfigurationManager in remya1000 .NET Framework 1.x 1 February 20th, 2008 09:18 AM
want to convert date from 01/12/07 to 12-Jan-2007 hurperl Perl 1 April 12th, 2007 05:47 AM
ConfigurationManager.ConnectionSettings.add() stubid_cat BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 3 February 25th, 2007 08:58 PM
Errors on Chapter 12 example(12.8) sonnie ASP.NET 2.0 Professional 2 June 7th, 2006 10:55 AM



All times are GMT -4. The time now is 04:45 PM.


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