Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 Display Modes
  #1 (permalink)  
Old February 1st, 2006, 03:07 PM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default datareader in c#

Hi Everyone,

I am having a real problem with something that seems so easy. I have an ASP.NET file which I have a seperate c# file. In my c# file I would like to connect to my database which is a SQL Server express 2005 db with web developer 2005.

Once connected to my db I would like to retrieve a value 'contact name' to a variable which I can then use in my insert. I started to use things like -

SqlDataSource contacts = new SqlDataSource();
contacts.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
contacts.SelectCommand = "SELECT contactName FROM tblContacts WHERE contactID = @tmpContactID";
contacts.SelectParameters.Add("tmpContactID", contactnameDropDownList.SelectedValue);

What I cannot seem to do is make this into a datareader and add to a variable (string).

I am banging my head against a wall now so please help.

Ryan
__________________
Thanks

Ryan
Reply With Quote
  #2 (permalink)  
Old February 2nd, 2006, 04:24 AM
Wrox Technical Editor
 
Join Date: Nov 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Are you using the apropriate SQL INSERT statement for your .Add function?

Example here:
http://msdn2.microsoft.com/system.we...ce.insert.aspx
Reply With Quote
  #3 (permalink)  
Old February 2nd, 2006, 04:39 AM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have just had a look at the MSDN and I am not sure if that is doing what I am trying to do. All I want to do is retrieve a value from a table in the database based on a parameter which I set 'tmpContactID'.

I may be doing this wrong as I am new to this.

Thanks

Ryan
Reply With Quote
  #4 (permalink)  
Old February 2nd, 2006, 04:51 AM
Wrox Technical Editor
 
Join Date: Nov 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

// Sample C# code accessing a sample database

// You need:
// A database connection
// A command to execute
// A data adapter that understands SQL databases
// A table to hold the result set

namespace DataAccess
{
    using System.Data;
    using System.Data.SqlClient;

    class DataAccess
    {
        //This is your database connection:
        static string connectionString = "Initial Catalog=northwind;Data Source=(local);Integrated Security=SSPI;";
        static SqlConnection cn = new SqlConnection(connectionString);

        // This is your command to execute:
        static string sCommand = "SELECT TOP 10 Lastname FROM Employees ORDER BY EmployeeID";

        // This is your data adapter that understands SQL databases:
        static SqlDataAdapter da = new SqlDataAdapter(sCommand, cn);

        // This is your table to hold the result set:
        static DataTable dataTable = new DataTable();

        static void Main()
        {
            try
            {
                cn.Open();

                // Fill the data table with select statement's query results:
                int recordsAffected = da.Fill(dataTable);

                if (recordsAffected > 0)
                    foreach (DataRow dr in dataTable.Rows)
                        System.Console.WriteLine(dr[0]);
            }
            finally
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
        }
    }
}

Would that do the trick? Just change your SELECT statement, throw in your desired condition. and it should return you the value..
Reply With Quote
  #5 (permalink)  
Old February 2nd, 2006, 03:18 PM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok I have started a new app with a new database,

I thought I better let you know a few things. First of all I am using SQLExpress2005 and have turned on NamedPipes and TCP/IP for my instance.

I used the below code in my cs file;

 //This is your database connection:
        string connectionString = "Data Source=SQLEXPRESS;AttachDbFilename=Customers.mdf;I ntegrated Security=True;User Instance=True";
        SqlConnection cn = new SqlConnection(connectionString);

        // This is your command to execute:
        string sCommand = "SELECT * FROM Customer";

        // This is your data adapter that understands SQL databases:
        SqlDataAdapter da = new SqlDataAdapter(sCommand, cn);

        // This is your table to hold the result set:
        DataTable dataTable = new DataTable();

                cn.Open();

                // Fill the data table with select statement's query results:
                int recordsAffected = da.Fill(dataTable);

                if (recordsAffected > 0)
                    foreach (DataRow dr in dataTable.Rows)
                        System.Console.WriteLine(dr[0]);
                if (cn.State != ConnectionState.Closed)
                    cn.Close();

When I run the page I get the following error which I thought I would fix by enabling Named pipes and TCP/IP.

Any ideas?

(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Thanks
Reply With Quote
  #6 (permalink)  
Old February 2nd, 2006, 03:36 PM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it, thanks for all your help it was all in the DB connection string. Here it is for you ref.

//This is your database connection:
        string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\External Disk\\Websites\\CallLogging\\App_Data\\Customers.m df;Integrated Security=True;Connect Timeout=30;User Instance=True";
            //"Data Source=SQLEXPRESS;AttachDbFilename=Customers.mdf;I ntegrated Security=True;User Instance=True";
        SqlConnection cn = new SqlConnection(connectionString);

        // This is your command to execute:
        string sCommand = "SELECT * FROM Customer";

        // This is your data adapter that understands SQL databases:
        SqlDataAdapter da = new SqlDataAdapter(sCommand, cn);

        // This is your table to hold the result set:
        DataTable dataTable = new DataTable();

                cn.Open();

                // Fill the data table with select statement's query results:
                int recordsAffected = da.Fill(dataTable);

                if (recordsAffected > 0)
                    foreach (DataRow dr in dataTable.Rows)
                        System.Console.WriteLine(dr[0]);
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
Reply With Quote
  #7 (permalink)  
Old June 30th, 2009, 12:48 AM
Authorized User
Points: 30, Level: 1
Points: 30, Level: 1 Points: 30, Level: 1 Points: 30, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# datareader samples

seee from the following linkj , some datareader samples

http://csharp.net-informations.com/d...datareader.htm

lee.
Reply With Quote
Reply


Thread Tools
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
datareader MunishBhatia ASP.NET 2.0 Professional 2 October 17th, 2007 07:05 AM
regarding datareader adityamadisetty VB.NET 1 May 8th, 2006 09:31 AM
DataReader truongnnhat ASP.NET 1.0 and 1.1 Basics 2 February 17th, 2005 11:41 PM
datareader surapongmax ADO.NET 1 October 8th, 2004 07:53 AM
DataReader cjcd BOOK: Beginning ASP.NET 1.0 2 March 21st, 2004 12:06 PM



All times are GMT -4. The time now is 05:40 PM.


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