Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
I want to control the value of textbox through comboox selection.My coding is:
C#
private void Form1_Load(object sender, EventArgs e)
       {
           SqlDataAdapter ad = new SqlDataAdapter("select *from Reg",con);
           DataSet ds = new DataSet();
           ad.Fill(ds, "Reg");
           comboBox1.DataSource = ds.Tables["Reg"].DefaultView;
           comboBox1.DisplayMember = "name";
           comboBox1.ValueMember = "name";

       }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           SqlDataAdapter ad = new SqlDataAdapter("select *from Reg where name='" + comboBox1.SelectedItem.ToString()+ "'", con);
           DataSet ds = new DataSet();
           ad.Fill(ds, "Reg");
           dataGridView1.DataSource = ds.Tables["Reg"].DefaultView;
           textBox1.Text = ds.Tables["Reg"].Rows[0]["Email"].ToString();

       }


But it is showing one error.That error is : There is no row at position 0.

Sir please give solution
Posted
Updated 3-Apr-11 20:42pm
v2
Comments
DaveAuld 4-Apr-11 2:43am    
Edit: Code Formatting added.
DaveAuld 4-Apr-11 2:46am    
Are there any rows returned by the query? if the answer to this is yes, them did you try increaing the index number to Rows[1] and see if that works? it might give you a clue to what the base index value is.
Banajyotsna 4-Apr-11 2:52am    
When i changed the index number to Rows[1] it is showing the error : There is no row at position 1.

http://social.msdn.microsoft.com/Forums/en/Vsexpressvb/thread/8844df20-9c56-47b5-8023-3d5b38d454d2[^]
I hope the above information will be helpful. If you have more concerns, please let me know.
 
Share this answer
 
Hi,

Please rechange the your code as follows.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  {
    SqlDataAdapter ad = new SqlDataAdapter("select *from Reg where name='" + comboBox1.SelectedItem.ToString()+ "'", con);
     DataSet ds = new DataSet();
     ad.Fill(ds, "Reg");
     dataGridView1.DataSource = ds.Tables["Reg"].DefaultView;
     if(ds.Tables["Reg"].Rows.Count > 0)
       {
            textBox1.Text = ds.Tables["Reg"].Rows[0]["Email"].ToString();
       }
    else
      {
        textBox1.Text="";
      }
            
   }

If the selected combo box value is not there in database,it will return zero rows.
Thats why you are getting this kind of error.
I hope it will solve your problem.

Regards,
Kiran.
 
Share this answer
 
v2
Comments
Banajyotsna 4-Apr-11 3:04am    
In combobox having data.When i used your coding it is not retrieving the data from database.But when i changed comboBox1.SelectedValue.ToString() in your coding it is working fine.only little i changed.that is:comboBox1.SelectedValue.ToString();
Thanks for giving path to me.Thanks a lot.It is working fine now.
Banajyotsna 4-Apr-11 3:08am    
thanks a lot sir
Monjurul Habib 4-Apr-11 3:04am    
code block(Edited.).
I think you should use
SqlDataAdapter ad = new SqlDataAdapter("select *from Reg where name='" + comboBox1.SelectedValue.ToString()+ "'", con);
in stead of
SqlDataAdapter ad = new SqlDataAdapter("select *from Reg where name='" + comboBox1.SelectedItem.ToString()+ "'", con);


Hope it helps, Good luck!

Eduard
 
Share this answer
 
Comments
Banajyotsna 4-Apr-11 2:50am    
It is showing same error only
Banajyotsna 4-Apr-11 2:51am    
when i used comboBox1.SelectedValue.ToString() then also it is showing same error.
Thanx i have been looking for that for the past 8 hours. Stay blessed
 
Share this answer
 

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