Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnok_Click(object sender, EventArgs e)
        {
           cmd = new SqlCommand("select Member_Name,Father_Name,Corresponding_address,Designation,ValidFrom,Validupto from EmployeeDetails where StaffNumber='" + lbstaffno.Text + "'", con);

            con.Open();
           da = new SqlDataAdapter("select Member_Name,Father_Name,Corresponding_address,Designation,ValidFrom,Validupto from EmployeeDetails where StaffNumber='" + lbstaffno.Text + "'", con);
            da.Fill(ds, "EmployeeDetails");
                dt = ds.Tables["EmployeeDetails"];
                DataView dv = new DataView(dt);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    lbstaffno.Text = dv[0]["StaffNumber"].ToString();
                    lbmembername.Text = dv[0]["Member_Name"].ToString();
                    lbfathersname.Text = dv[0]["Father_Name"].ToString();
                    lbdesignation.Text = dv[0]["Designation"].ToString();
                    lbaddress.Text = dv[0]["Corresponding_address"].ToString();

                    con.Close();
                }

        }
Posted
Updated 26-Sep-11 23:28pm
v2
Comments
hitech_s 27-Sep-11 5:26am    
what is the datatype of "StaffNumber" in your database..
P.Salini 27-Sep-11 5:38am    
you will get this error when you are trying to convert nonnumeric value to int so check the value in lbstaffno.Text
OriginalGriff 27-Sep-11 5:39am    
Do not re-post your question to add further information - use the "Improve question" widget to edit your question and provide better information instead.
I have deleted the older version without the code fragment.

C#
da = new SqlDataAdapter("select Member_Name,Father_Name,Corresponding_address,Designation,ValidFrom,Validupto from EmployeeDetails where StaffNumber='" + lbstaffno.Text + "'", con);

if your StaffNumber is of int then try this query
C#
da = new SqlDataAdapter("select Member_Name,Father_Name,Corresponding_address,Designation,ValidFrom,Validupto from EmployeeDetails where StaffNumber='" + Convert.toInt32(lbstaffno.Text) + "'", con);


or any thing else then simply note the line number or place a query where you got the error and also provide your database structure so we understand that which datatype of your StaffNumber have
 
Share this answer
 
v2
Comments
OriginalGriff 27-Sep-11 6:10am    
Um.
All that is going to do is introduce a different possible conversion error and another conversion to string (as the string concatenation will put an implicit "ToString" on the converted integer).
The problem is in the Query!
You've written
SQL
cmd = new SqlCommand("select Member_Name,Father_Name,Corresponding_address,Designation,ValidFrom,Validupto from EmployeeDetails where StaffNumber='" + lbstaffno.Text + "'", con);

con.Open();
           da = new SqlDataAdapter("select Member_Name,Father_Name,Corresponding_address,Designation,ValidFrom,Validupto from EmployeeDetails where StaffNumber='" + lbstaffno.Text + "'", con);

The problem is that while stating an int value in coding, we never enclose them in quotes!
All you have to do is just remove the single quotes from your query and you're done!
Change your query in the following manner:
SQL
cmd = new SqlCommand("select Member_Name,Father_Name,Corresponding_address,Designation,ValidFrom,Validupto from EmployeeDetails where StaffNumber=" + lbstaffno.Text, con);

con.Open();
           da = new SqlDataAdapter("select Member_Name,Father_Name,Corresponding_address,Designation,ValidFrom,Validupto from EmployeeDetails where StaffNumber=" + lbstaffno.Text, con);
 
Share this answer
 
v2
You aren't converting it to an int - you are converting it to a string, and storing it in a labels Text property.

If you want to convert it to an int, then either:
1) Use the relevant datatype in your database (i.e. int) and cast it when you access the field:
C#
int i = (int) dv[0]["StaffNumber"];
Or
2) Use the int.Parse or int.TryParse methods to convert the string to an int.
 
Share this answer
 
Comments
P.Salini 27-Sep-11 5:45am    
By watching the code i think the problem is near query.
please check the code once.
In you select query you are directly comparing your lbstaffno.Text without converting. First you need to convert it to int then compare
 
Share this answer
 
Conversion failed when

converting the varchar value 'StaffNumber'


to data type int

means weather your column StaffNumber is varchar field and you are trying to

convert to int type without parsing it

User Cast (StaffNumber as int) as StaffNumber

your problem has been solved... :)
 
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