473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sql data to string

Hi,

I need to fill a string with data from sql server, i am using the following
statement for retrieving the data (example)

public SqlDataReader Inleiding ()
{

strConn="Data Source=W2K3-BASE;uid=test;pwd=test;Initial
Catalog=northwind";
mycn = new SqlConnection(strConn);
SqlCommand myda = new SqlCommand ("Select description FROM Categories
where categoryid = 1 ", mycn);
myda.CommandType = CommandType.Text;
mycn.Open();
SqlDataReader ddsqlReader =
myda.ExecuteReader(CommandBehavior.CloseConnection );
return ddsqlReader;
}

But when I fill the string with the ddsql reader it gives me a true value
and not the value of the record! How can I solve this?

Filling the string like this:

string test = Testing().ToString();

--
thanks,

Remco Ploeg
Nov 19 '05 #1
3 1219
Right...

That is because you are essentially returning a record set. The data reader
gives you one row of data at a time, in a readonly, forwardonly fashion.
Calling ToString on it, doesn't really make logical sense.

Additionally, you have a connection leak, since the data reader never gets
closed in your code.

Use ExecuteScalar when you need one cell worth of data (first column of
first row returned by query). And always close connections before you exit
the method that opened the connection (unless you are very careful about
closing them afterwards, but that doesn't appear to be the case).

"Remco Ploeg" <Re********@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
Hi,

I need to fill a string with data from sql server, i am using the
following
statement for retrieving the data (example)

public SqlDataReader Inleiding ()
{

strConn="Data Source=W2K3-BASE;uid=test;pwd=test;Initial
Catalog=northwind";
mycn = new SqlConnection(strConn);
SqlCommand myda = new SqlCommand ("Select description FROM Categories
where categoryid = 1 ", mycn);
myda.CommandType = CommandType.Text;
mycn.Open();
SqlDataReader ddsqlReader =
myda.ExecuteReader(CommandBehavior.CloseConnection );
return ddsqlReader;
}

But when I fill the string with the ddsql reader it gives me a true value
and not the value of the record! How can I solve this?

Filling the string like this:

string test = Testing().ToString();

--
thanks,

Remco Ploeg

Nov 19 '05 #2
ok thanks, i will give it a try. And i will close the connection!
--
thanks,

Remco Ploeg
"Marina" wrote:
Right...

That is because you are essentially returning a record set. The data reader
gives you one row of data at a time, in a readonly, forwardonly fashion.
Calling ToString on it, doesn't really make logical sense.

Additionally, you have a connection leak, since the data reader never gets
closed in your code.

Use ExecuteScalar when you need one cell worth of data (first column of
first row returned by query). And always close connections before you exit
the method that opened the connection (unless you are very careful about
closing them afterwards, but that doesn't appear to be the case).

"Remco Ploeg" <Re********@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
Hi,

I need to fill a string with data from sql server, i am using the
following
statement for retrieving the data (example)

public SqlDataReader Inleiding ()
{

strConn="Data Source=W2K3-BASE;uid=test;pwd=test;Initial
Catalog=northwind";
mycn = new SqlConnection(strConn);
SqlCommand myda = new SqlCommand ("Select description FROM Categories
where categoryid = 1 ", mycn);
myda.CommandType = CommandType.Text;
mycn.Open();
SqlDataReader ddsqlReader =
myda.ExecuteReader(CommandBehavior.CloseConnection );
return ddsqlReader;
}

But when I fill the string with the ddsql reader it gives me a true value
and not the value of the record! How can I solve this?

Filling the string like this:

string test = Testing().ToString();

--
thanks,

Remco Ploeg


Nov 19 '05 #3
ok here my new code, it seems to work:

public string Inleiding ()
{

SqlConnection myConn = new SqlConnection ("Data
Source=W2K3-BASE;uid=test;pwd=test;Initial Catalog=northwind" );

// define the command query
string query = "Select description FROM Categories where categoryid = 1";

// initialize command object with the specified query and connection
SqlCommand myCommand = new SqlCommand ( query, myConn );

// open the data connection
myConn.Open ( );

// execute the command
string test = myCommand.ExecuteScalar().ToString();

// close the data connection
myConn.Close ( );

return test.ToString();

}

--
thanks,

Remco Ploeg
"Marina" wrote:
Right...

That is because you are essentially returning a record set. The data reader
gives you one row of data at a time, in a readonly, forwardonly fashion.
Calling ToString on it, doesn't really make logical sense.

Additionally, you have a connection leak, since the data reader never gets
closed in your code.

Use ExecuteScalar when you need one cell worth of data (first column of
first row returned by query). And always close connections before you exit
the method that opened the connection (unless you are very careful about
closing them afterwards, but that doesn't appear to be the case).

"Remco Ploeg" <Re********@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
Hi,

I need to fill a string with data from sql server, i am using the
following
statement for retrieving the data (example)

public SqlDataReader Inleiding ()
{

strConn="Data Source=W2K3-BASE;uid=test;pwd=test;Initial
Catalog=northwind";
mycn = new SqlConnection(strConn);
SqlCommand myda = new SqlCommand ("Select description FROM Categories
where categoryid = 1 ", mycn);
myda.CommandType = CommandType.Text;
mycn.Open();
SqlDataReader ddsqlReader =
myda.ExecuteReader(CommandBehavior.CloseConnection );
return ddsqlReader;
}

But when I fill the string with the ddsql reader it gives me a true value
and not the value of the record! How can I solve this?

Filling the string like this:

string test = Testing().ToString();

--
thanks,

Remco Ploeg


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
1
by: Andrew Arace | last post by:
I scoured the groups for some hands on code to perform the menial task of exporting table data from an Access 2000 database to Oracle database (in this case, it was oracle 8i but i'm assuming this...
2
by: Fatih BOY | last post by:
Hi, I want to send a report from a windows application to a web page like 'report.asp' Currently i can send it via post method with a context like local=En&Username=fatih&UserId=45&Firm=none...
1
by: T8 | last post by:
I have a asp.net (framework 1.1) site interfacing against SQL 2000. It runs like a charm 99% of the time but once in a while I get the following "unspecified error". Sometimes it would resolve by...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
6
by: Tejpal Garhwal | last post by:
I have datagrid filled with some data rows. At the run time i want know how many total rows are there in the data grid ? Any idea ? Any Suggestions ? Thanks in advance Tej
1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
1
by: aemado | last post by:
I am trying to read in several lines, each should have exactly 5 pieces of data. I am using try/catch/throw to determine if the data is in the correct format, and trying to use iss to separate the...
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.