Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is when i select Bangalore and mumbai From 2 different combobox i get the output as individual data / single data from db, while i have given serveral data in data base and it gives only one. for example i have several flights from bangalore to mumbai with different timings so i need help to display all the timings.

What I have tried:

C#
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection("Data Source=.;AttachDbFilename=C:\\Program Files\\Microsoft SQL Server\\MSSQL12.MSSQLSERVER\\MSSQL\\DATA\\enq.mdf;Integrated Security=True;Connect Timeout=30");
    con.Open();
    String str = "select * from Air where Air_To = '" + comboBox2.Text + "'and Air_From = '" + comboBox1.Text + "'";
    SqlCommand cmd = new SqlCommand(str, con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
    {
        textBox1.Text = dr["Air_dist"].ToString();
        richTextBox1.Text = dr["Air_departure"].ToString();

    }
Posted
Updated 6-Mar-17 2:56am
v2
Comments
_Asif_ 6-Mar-17 2:55am    
can you share your table structure and sample data?
Richard MacCutchan 6-Mar-17 3:11am    
No, it is your code that retrieves only one from the returned result set.

1 solution

Quote:
i need help to display all the timings.


If you want to display more than one line item from sql table then you should a Gridview or repeater Control, instead of a textbox.

from your code
Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

replace the SqlDataReader with SqlDataAdapter and bind it to a Gridview or repeater control
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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