Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I want to get the details of employee in textbox on combobox selected index changed event. I have done the following code. but when I run the project it shows an error that there is no row at position 0.
Thanks in Advance !!!

C#
private void cmbEmp_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string F_name, L_name, dob, Desig, dept, join_date;
            //string var = cmbEmp_ID.SelectedValue.ToString();
            //fillData(var);
            string a = cmbEmp_ID.SelectedValue.ToString();
            fillData(a);
        }

        public void fillData(string lvVar)
        {
            string cnstr = ConfigurationManager.ConnectionStrings["abc_ConnectionString"].ConnectionString;
            SqlConnection cont = new SqlConnection(cnstr);
            SqlDataAdapter adp = new SqlDataAdapter("Select First_Name, Last_Name, DOB, Designation, Department, Join_Date from Employee where Emp_ID = '" + lvVar + "'", cont);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            DataTable dt = ds.Tables[0];
            txtFirst_Name.Text = dt.Rows[0]["First_Name"].ToString();
            txtLast_Name.Text = dt.Rows[0]["Last_Name"].ToString();
        }
Posted
Updated 9-Feb-12 23:09pm
v2

C#
private void cmbEmp_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cmbEmp_ID.SelectedIndex != -1) //Check For This First
            {
                string a = cmbEmp_ID.SelectedValue.ToString();
                fillData(a);
            }
        }


If Any Problem Than Ask Again
 
Share this answer
 
Comments
Ank_ush 11-Feb-12 11:18am    
When I run the project, the actual value is not passed to textbox, Value passed is System.Dataview.something.
Aniket Yadav 11-Feb-12 23:50pm    
private void cmbEmp_ID_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbEmp_ID.SelectedIndex != -1) //Check For This First
{
string a = cmbEmp_ID.SelectedText.ToString(); // Try This
fillData(a);
}
}
I think the problem is with emp_id column. You are considering it as String but in the Database you might have its datatype as Integer/ Number.

If the emp_id column is numeric then use -
C#
Convert.ToInt32(lvVar);

in your code as-
C#
SqlDataAdapter adp = new SqlDataAdapter("Select First_Name, Last_Name, DOB, Designation, Department, Join_Date from Employee where Emp_ID = " + Convert.ToInt32(lvVar) + "", cont);

Otherwise rest of the code is correct i think...
 
Share this answer
 
I think you are getting error on below line of code.
CSS
txtFirst_Name.Text = dt.Rows[0]["First_Name"].ToString();

check the row count of the datatable before getting the row from datatable.
And also cmbEmp_ID.SelectedValue should not be a null value.
 
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