Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to fetch all data of a column in textbox from database in form of list like combobox.
i am trying this code .................


C#
CON.Open();
SqlCommand CMD = new SqlCommand("SELECT CUST_NAME FROM CUSTOMER WHERE CUST_ID=CUST_NAME", CON);
SqlDataReader DR = CMD.ExecuteReader();
while (DR.Read())
{
   CUST_NAME.Text = DR["CUST_NAME"].ToString();
}
CON.Close();


[edit]Code block added[/edit]
Posted
Updated 24-Jun-13 23:05pm
v2

you can get comma separated value as below in textbox..

C#
CON.Open();
SqlCommand CMD = new SqlCommand("SELECT CUST_NAME FROM CUSTOMER WHERE CUST_ID=CUST_NAME", CON);
SqlDataReader DR = CMD.ExecuteReader();
while (DR.Read())
{
    CUST_NAME.Text += DR["CUST_NAME"].ToString();
}
CON.Close();
 
Share this answer
 
Comments
arvindnitin7 25-Jun-13 3:15am    
ISSE SE NAHI HUA
GIVE ME MORE SOLUTIONS
KiranKumar Roy 25-Jun-13 4:51am    
what output you want tell me.. above function will give you comma separated result in your texbox like Kiran,Kumar,Roy
Well, it won't work, unless your customer id is "CUST_NAME".
Try this:
C#
CON.Open();
SqlCommand CMD = new SqlCommand("SELECT CUST_NAME FROM CUSTOMER WHERE CUST_ID=@ID", CON);
CMD.Parameters.AddWithValue("@ID", myCustomerIDThatIAmLookingFor);
SqlDataReader DR = CMD.ExecuteReader();
if (DR.Read())
   {
   CUST_NAME.Text = DR["CUST_NAME"].ToString();
   }
CON.Close();
Note that I also changed your while loop to an if - since you overwrite the text in the box each time a loop is probably not what you want...
 
Share this answer
 
Comments
arvindnitin7 25-Jun-13 3:11am    
MR.GRIFF ,
THIS IS RIGHT , BUT I AM TRYING ALL CUST_NAME IN TEXTBOX LIKE COMBOBOX
I WANT, WHEN I AM WRITE 1st CHARACTOR OF MY NAME THEN WHOLE NAME WILL BE DISPLAY
OriginalGriff 25-Jun-13 3:19am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
arvindnitin7 25-Jun-13 3:26am    
ok sir,
so sorry,but i am trying cust_name in textbox like combobox
OriginalGriff 25-Jun-13 3:52am    
Doing it in a textbox is a pain - it's possible, but you have to handle selection and so forth in the same way that a combobox does, and given that a text box is generally single line it's not easy for the user to work with. If you make it multiline, then at least he can see the options, but...a text box is not a good choice for this. I would probably either use a list and a textbox (so the user enters in a TB and the results appear in the list) or use a combobox with a databinding instead.

Is there a very good reason for using a textbox for this?
arvindnitin7 25-Jun-13 3:56am    
thanx sir
C#
//search Details

SqlDataAdapter retail = new SqlDataAdapter("SELECT retailPrice FROM AddItem WHERE itemCode='" + comboBox2.Text + "'", DB.connection());

//create DataTable
            DataTable DT3 = new DataTable();
//Fill DataTable
            retail.Fill(DT3);
//Search Details Decimal
            decimal cat2 = (decimal)DT3.Rows[0][0];
//Fill textBox
            textBoxretail.Text = Convert.ToString(cat2);


[edit]Code block added[/edit]
 
Share this answer
 
v2

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