Click here to Skip to main content
15,888,121 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am retrieving data from differetnt tables and I want to show in textboxes in C# first textbox is filling from database but 2nd 1 is not showing my code is
C#

string str = "Select Address from AddressInfo where Contactid=(Select Contactid from ContactInfo where Fid=(Select Fid from Farmers where Name='" + txtUpName.Text + "')); Select PhoneNo from PhoneInfo where Contactid=(Select Contactid from ContactInfo where Fid=(Select Fid from Farmers where Name='" + txtUpName.Text + "')); ";
                con.Open();
                SqlCommand command = new SqlCommand(str, con);
                SqlDataReader dr = command.ExecuteReader();
                dr.Read()
                
                    if (dr.HasRows)
                    {
                        txtUpAddress.Text = dr[0].ToString();
                        txtUpPhoneNo.Text = dr[1].ToString();
                        con.Close();
                    }

                    else
                    {
                        MessageBox.Show("No data recieved ");
                    }
Posted

Since you are selecting only a single column (Address) from your database table, your SqlDataReader will only have one column, so accessing dr[1] will always give you an exception.

Try adding the phone number to your SELECT query!
 
Share this answer
 
Comments
Mohammed Hameed 18-May-13 6:15am    
omg, he didnt specify about exception. Anyhow thanks for solution, +5d.
OriginalGriff 18-May-13 6:25am    
You're welcome!
Your query should be something like :

C#
string str = " Select Address,PhoneNo from AddressInfo A,PhoneInfo P where A.Contactid = P.Contactid and A.Contactid=(Select Contactid from ContactInfo where Fid=(Select Fid from Farmers where Name='" + txtUpName.Text + "'));"; 
 
Share this answer
 
Comments
Zain ul abdeen 18-May-13 6:16am    
Did'nt understand this method nut its working fine :)
Mayur Panchal 18-May-13 6:21am    
Nothing special in it. There's a join of two tables in above query.
Possible causes could be:

1. dr[1].ToString() is null.
(OR)
2. txtUpPhoneNo.text value is getting nullified somewhere.

If any queries do let me know...
 
Share this answer
 
v2
Comments
OriginalGriff 18-May-13 6:04am    
Simpler than that: He only selects a single column from teh DB, so his reader will always error on dr[1]...:laugh:
you are executing two select queries so the phoneno will not come in first Result.

Either you combine two select queries by joining or use dr.NextResult() for fetching the next result.
 
Share this answer
 
v2
Comments
Zain ul abdeen 18-May-13 6:51am    
txtUpCompanyName.Text = dr.NextResult().ToString(); is this right?
Arwinder Singh 18-May-13 7:27am    
No

if(dr.NextResult())
{
if (dr.HasRows)
{
txtUpPhoneNo.Text = dr[0].ToString();
}
}

dr.NextResult() will help you to move your datareader to the next set of result.
In your case its better to Combine your select query.

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