Click here to Skip to main content
14,733,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to put a column value from sql server database that it was get from a query into a string(notice that this query answer is only one column) and i want to put this answer in a string to compare it with a value

what i must do?

1 solution

you can use ExecuteScalar method as:
string value = (string)command.ExecuteScalar();

check below link for more information[^]
_Starbug_ 1-Jul-12 7:19am
NO that was not work for me.still my string has value of null with your code and i go that link but it also do not solving my problem.
DamithSL 1-Jul-12 7:28am
can you show the code what you try?
_Starbug_ 1-Jul-12 8:05am
yes.AMOZESHEMPLOYEE is a table in my database and EID,PASSWORD is a column of that

SqlCommand da=new SqlCommand("Select EID from AMOZESHEMPLOYEE WHERE EID='1001'",con1);
SqlCommand da2=new SqlCommand("Select PASSWORD from AMOZESHEMPLOYEE WHERE PASSWORD='12345'",con1);
string a = (string)da.ExecuteScalar();
string b = (string)da2.ExecuteScalar();
DamithSL 1-Jul-12 8:14am
both a add b are null? run both your sql statements in sql server and check how many records it will return. if it is more than one then you need to use sql reader and get value from that
_Starbug_ 1-Jul-12 8:57am
no both da,da2 in sql server management studio returns only one record with one column.but in c# both of a,b returns null value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900