Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get particulat row from a Table
my Table is Programming language and got 2 columns Name and Id
ID  Name
1    c
2    c++
3    java...

now i want a id from this table like where Name = C
Name i had in combobox and save as a string
C#
String Name = combobox.text;

upto here i got a name if a column now i want to fetch ID here is my cod
string query = " SELECT ID FROM Programming WHERE Name = " + Name ;
C#
SqlCeCommand cmd;
con = new SqlCeConnection(@Data Source=|DataDirectory|\Db\StudentDB.sdf;);
                con.Open();
                cmd = new SqlCeCommand(query, con);

                connection.Open();
              
                sAdapter = new SqlCeDataAdapter(cmd);  //sAdapter is sqlce Adapter

                sBuilder = new SqlCeCommandBuilder(sAdapter); //sBuilder command builder

                sDs = new DataSet();
                sAdapter.Fill(sDs, Programming;) //Programming is Name of my table
                //After this line is thrwong exception
                sD_Table = sDs.Tables[Programming;];
                connection.Close();


Buttom line is i want just ID from table where Name = combobox.text;
How can i fetch just a ID????
Posted
Updated 23-Feb-15 1:35am
v4

Hi,

Instead of combobox you can use the dropdownlist. In dropdownlist you can directly store the ID values in the dataValue field. In this way while bonding the dropdownlist you can have both ID and the Name values binded together and you can access where ever you want the ID value to be refered.

If you are using the combobox also you can change the combobox to dropdownlist easily.

Here you can mention the query as "select ID from Programming where Name = '" + Name +"'" and your result will return you the ID value.

Use any one of the method mentioned above and your work will be done.

Thanks
Sisir Patro
 
Share this answer
 
v2
Comments
Member 11000455 23-Feb-15 6:52am    
its not working
[no name] 23-Feb-15 7:41am    
Updated the solution. Check
sAdapter.Fill(sDs, Programming;) //Programming is Name of my table
                //After this line is thrwong exception


why you mentioned semicolon(";")

Instead try to do the following way

sAdapter.Fill(sDs, "Programming")
 
Share this answer
 
Comments
Member 11000455 23-Feb-15 6:51am    
why you mentioned semicolon(";")
It is just Typo mistake
already tried without semicolon
Sandesh M Patil 23-Feb-15 7:11am    
also add double quotes
Member 11000455 23-Feb-15 7:32am    
already done but problem is my query its throwing message
Mt table
for eg:
ID Name
1 c
2 c#
string query = " SELECT ID FROM Programming WHERE Name = " + Name ;
Member 11000455 23-Feb-15 7:33am    
und + Name here Name is every time correct
but i cant figeru out whats wrong seems so simple but not working
[no name] 23-Feb-15 7:39am    
the query should be like "select ID from Programme where Name = '" + Name "'"; Use this onw and it will work.

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