Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string query = "Select * from ACCOUNTS where Account_number ='" + comboBox1.SelectedIndex + "'";
           da = new SqlDataAdapter(query, cn);
           DataTable dt1 = new DataTable();
           da.Fill(dt1);


               txtcustomername.Text = dt1.Rows[0]["Name"].ToString();
               txtoccupation.Text = dt1.Rows[0]["Occupation"].ToString();
               txtfathername.Text = dt1.Rows[0]["Father_name"].ToString();
               txthusbandname.Text = dt1.Rows[0]["Husband_name"].ToString();
               txtcustomercnic.Text = dt1.Rows[0]["Customer_CNIC"].ToString();
               txtinitialdeposit.Text = dt1.Rows[0]["Initial_deposit"].ToString();
               dateTimePicker1.Value = Convert.ToDateTime(dt1.Rows[0]["Date"]);
               txtpostaladdress.Text = dt1.Rows[0]["Postal_address"].ToString();
               txtemail.Text = dt1.Rows[0]["Mail"].ToString();
               txtmobilenumber.Text = dt1.Rows[0]["Mobile"].ToString();


           }
Posted
Comments
__TR__ 4-Dec-12 1:22am    
What is the error message you got?

In order to help you with your question you should post the error you get.

But in the mean time, few things.

Never ever concatenate values directly to your Sql statement. Use SqlParameter[^].

Another thing, are you sure that the SelectedIndex of the combo box is really the Account_number in your database. This looks really odd... Should you use the selected value instead?
 
Share this answer
 
Comments
__TR__ 4-Dec-12 1:36am    
My 5!
Even I thought the same thing. We almost answered at the same time :)
Thanks for pointing out to use parametrized query. I missed that.
Wendelius 4-Dec-12 13:47pm    
Thanks :)
Hi Zeshan,

Update your code as below.

C#
string query = "Select * from ACCOUNTS where Account_number ='" + comboBox1.SelectedIndex + "'";
SqlCommand cmd = new SqlCommand(query,cn);
da = new SqlDataAdapter(cmd);
DataSet ds= new DataSet();
da.Fill(ds);
if(ds.Table[0].Rows.Count>0)
{         
    txtcustomername.Text = ds.Table[0].Rows[0]["Name"].ToString();
    txtoccupation.Text = ds.Table[0].Rows[0]["Occupation"].ToString();
    txtfathername.Text = ds.Table[0].Rows[0]["Father_name"].ToString();
    txthusbandname.Text = ds.Table[0].Rows[0]["Husband_name"].ToString();
    txtcustomercnic.Text = ds.Table[0].Rows[0]["Customer_CNIC"].ToString();
    txtinitialdeposit.Text = ds.Table[0].Rows[0]["Initial_deposit"].ToString();
    dateTimePicker1.Value = Convert.ToDateTime(ds.Table[0].Rows[0]["Date"]);
    txtpostaladdress.Text = ds.Table[0].Rows[0]["Postal_address"].ToString();
    txtemail.Text = ds.Table[0].Rows[0]["Mail"].ToString();
    txtmobilenumber.Text = ds.Rows[0]["Mobile"].ToString();
}
 
Share this answer
 
v2
Comments
__TR__ 4-Dec-12 1:27am    
You have posted your solution twice. Please remove one of them.
zeshanazam 4-Dec-12 1:29am    
data of corresponding account number is not displaying in text boxes. no error but got no results as well.
__TR__ 4-Dec-12 1:34am    
Check my solution explaining what might be wrong with your code.
Mohd. Mukhtar 4-Dec-12 1:41am    
I am not sure how it happen, I was just improving the solution But it got submitted as new solution.

Anyways Thanks :-)
zeshanazam 4-Dec-12 1:49am    
i have tried everything, Selectedvalue, selectedtext, but got no results.
Looking at your code one thing i noticed is you are using selectedIndex property of your combobox. This might the reason you are not getting any result because SelectedIndex Property[^] returns the index of the selected Item. I think you need to use the SelectedItem.Text[^] property if you want to get the text of the selected item in your combobox. If you are looking to retrieve the value of the selected Item then you need to use SelectedItem.value[^] property.

Let me know if this fixes your issue.
 
Share this answer
 
Comments
zeshanazam 4-Dec-12 2:03am    
i have tried everything, Selectedvalue, selectedtext, but got no results.
__TR__ 4-Dec-12 2:06am    
Is the data table getting populated with data in debug mode?
zeshanazam 4-Dec-12 2:19am    
here is my code...


private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
string query = "Select * from ACCOUNTS where Account_number ='" + comboBox1.SelectedText + "'";
da = new SqlDataAdapter(query, cn);
dt1 = new DataTable();
da.Fill(dt1);
if (dt1.Rows.Count== 0)
{
MessageBox.Show("ACCOUNT'" + comboBox1.Text + "' does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
txtcustomername.Text = dt1.Rows[0]["Name"].ToString();
txtoccupation.Text = dt1.Rows[0]["Occupation"].ToString();
txtfathername.Text = dt1.Rows[0]["Father_name"].ToString();
txthusbandname.Text = dt1.Rows[0]["Husband_name"].ToString();
txtcustomercnic.Text = dt1.Rows[0]["Customer_CNIC"].ToString();
txtinitialdeposit.Text = dt1.Rows[0]["Initial_deposit"].ToString();
dateTimePicker1.Value = Convert.ToDateTime(dt1.Rows[0]["Date"]);
txtpostaladdress.Text = dt1.Rows[0]["Postal_address"].ToString();
txtemail.Text = dt1.Rows[0]["Mail"].ToString();
txtmobilenumber.Text = dt1.Rows[0]["Mobile"].ToString();
}
__TR__ 4-Dec-12 2:27am    
Its not possible for me to debug your code as I don't have the database and other controls you would have used. So one option to check why the data is not getting populated is to debug your code.
In debug mode try to find out the value of the string "query" in your code and then execute that value in SQL server to see if data is getting populated. If data is retrieved for that query check to see if the data table is being loaded correctly with data.

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

  Print Answers RSS


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