473,386 Members | 1,758 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,386 software developers and data experts.

Select query that should return multiple rows: It does not

The given should return A,B that is two rows because both
accountnames A and B have the same user_Id=1 but it is only returning
A when I performed the op. MessageBox.Show("AccountNames:" +
Object.ToString() );

[WebMethod]
public Object GetAccountNames(string User_Id)
{
//string AccountNames;
Object AccountNames;

// create a Command,Connection and Reader object

System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection(connectionStri ng);
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand();
//Object returnValue;

// Search for the pincode

cmd.CommandText = "SELECT acc_name FROM Account_Information
where user_id='" + User_Id + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
AccountNames = cmd.ExecuteScalar();
//AccountNames = returnValue.ToString();
sqlConnection1.Close();

return AccountNames;

}

Mar 27 '07 #1
3 2955
ExecuteScalar is the wrong choice here.

You want

..ExecuteReader

or

LoadDataSet
ExecuteScalar is for 1 single value.

like

Select count(*) as MyCount from dbo.Emp

would be a single value. ExecuteScalar is for that kind of purpose.

"weird0" <am********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
The given should return A,B that is two rows because both
accountnames A and B have the same user_Id=1 but it is only returning
A when I performed the op. MessageBox.Show("AccountNames:" +
Object.ToString() );

[WebMethod]
public Object GetAccountNames(string User_Id)
{
//string AccountNames;
Object AccountNames;

// create a Command,Connection and Reader object

System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection(connectionStri ng);
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand();
//Object returnValue;

// Search for the pincode

cmd.CommandText = "SELECT acc_name FROM Account_Information
where user_id='" + User_Id + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
AccountNames = cmd.ExecuteScalar();
//AccountNames = returnValue.ToString();
sqlConnection1.Close();

return AccountNames;

}

Mar 27 '07 #2

I might have misread.

Are you expecting one and only one value?

See here:

http://msdn2.microsoft.com/en-us/library/ms978510.aspx
and scroll down to the appendix
Appendix
How to Enable Object Construction for a .NET Class
keep scrolling down a little, basically "every scenario" is listed there.

1 row, multiple columns
1 row, 1 value (what you want)
multi rows, multi columns

"sloan" <sl***@ipass.netwrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
ExecuteScalar is the wrong choice here.

You want

.ExecuteReader

or

LoadDataSet
ExecuteScalar is for 1 single value.

like

Select count(*) as MyCount from dbo.Emp

would be a single value. ExecuteScalar is for that kind of purpose.

"weird0" <am********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
The given should return A,B that is two rows because both
accountnames A and B have the same user_Id=1 but it is only returning
A when I performed the op. MessageBox.Show("AccountNames:" +
Object.ToString() );

[WebMethod]
public Object GetAccountNames(string User_Id)
{
//string AccountNames;
Object AccountNames;

// create a Command,Connection and Reader object

System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection(connectionStri ng);
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand();
//Object returnValue;

// Search for the pincode

cmd.CommandText = "SELECT acc_name FROM Account_Information
where user_id='" + User_Id + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
AccountNames = cmd.ExecuteScalar();
//AccountNames = returnValue.ToString();
sqlConnection1.Close();

return AccountNames;

}


Mar 27 '07 #3
In addition to what Sloan pointed out, you really want to get into the habit
of using parmeterized queries or even better, stored procedures.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"weird0" wrote:
The given should return A,B that is two rows because both
accountnames A and B have the same user_Id=1 but it is only returning
A when I performed the op. MessageBox.Show("AccountNames:" +
Object.ToString() );

[WebMethod]
public Object GetAccountNames(string User_Id)
{
//string AccountNames;
Object AccountNames;

// create a Command,Connection and Reader object

System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection(connectionStri ng);
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand();
//Object returnValue;

// Search for the pincode

cmd.CommandText = "SELECT acc_name FROM Account_Information
where user_id='" + User_Id + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
AccountNames = cmd.ExecuteScalar();
//AccountNames = returnValue.ToString();
sqlConnection1.Close();

return AccountNames;

}

Mar 27 '07 #4

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

Similar topics

2
by: cs8404 | last post by:
I cannot quite figure out how to accomplish the following results. My table is "Products" with the following fields: ID Item Metal Size Price 1 Ring 18ctGold 4-7 $23.00...
3
by: Bruno Panetta | last post by:
Suppose I have a table Orders with the following fields: OrderID ProductID OrderDate I would like a SELECT query to return those rows with multiple values for both ProductID and...
8
by: Blake | last post by:
Hello, I have a database at work that keeps an inventory of cameras by location. Every week I get a repair estimate sheet that lists cameras by serial number that need to be repaired. This list...
2
by: jennk | last post by:
i am working in Access 97, our database tables are linked from ODBCsqlsvr (not even sure what that means). i have a table where each record has a unique customer and their order information. there...
11
by: giloosh | last post by:
i would like to filter out a bunch rows from a table of sql row results. i have a checkbox on each row named checkbox (x = the current row id) i will submit the form and take all the rows that...
4
by: zergziad | last post by:
Hi All, I need help on how to return the rows in one particular field as a string with comma separated in between those data. For example: Table A ID Name IsActive...
4
by: raaman rai | last post by:
Pls see the code below, where i wanted to select all the records from my table where the submdate (submission date) is greater or equal to today's date. Whereas i have two or three records in my...
5
bilibytes
by: bilibytes | last post by:
Hi, i am having big headaches in trying to find out how to perform a very very simple query. having a table like this one: +--------------+----------+---------+ | name | object1 |...
11
by: Simon Bernard | last post by:
Hi there I am trying to create a database that generates delivery notes. I have an excel spreadsheet linked to the database. From the linked information I need to be able to create a delivery...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.