Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button1_Click(object sender, EventArgs e)
{
    Staff s = new Staff();
    staffAG j = new staffAG();
    j.id = comboBox2.Text; error in this row "cannot implicitly convert type string to int" 
    j.name = textBox2.Text;
    j.age = textBox3.Text;
    j.phone = textBox4.Text;
    j.address = textBox5.Text;
    j.post = comboBox1.Text;
    s.updatestaff(j);
    MessageBox.Show("Record updated");
}


coding of my class staff is
C#
public void updatestaff(staffAG j)
{
    int z = 0;
    cn.Open();
    cmd = new SqlCommand("update staff set name = @name,age = @age,phoneno = @phoneno,address = @address,post = @post where id = @id", cn);
    cmd.Parameters.AddWithValue("@id",Convert. ToString (j.id));
    cmd.Parameters.AddWithValue("@name",j.name);
    cmd.Parameters.AddWithValue("@age",j.age);
    cmd.Parameters.AddWithValue("@phoneno",j.phone);
    cmd.Parameters.AddWithValue("@address",j.address);
    cmd.Parameters.AddWithValue("@post",j.post);
    cmd.ExecuteNonQuery();
    cn.Close();
    {
        MessageBox.Show("Added");
    }
}


coding of my class staffAG is

C#
class staffAG
{
int _id;
string _name;

string _age;
string _phone;
string _address;
string _post;
string _gender;
public string name
{
    get
    {
        return _name;
    }
    set
    {
        _name = value;
    }
}
public int id
{
    get
    {
        return _id;
    }
    set
    {
        _id = value;
    }
}
public string age
{
    get
    {
        return _age;
    }
    set
    {
        _age = value;
    }
}
public string phone
{
    get
    {
        return _phone;
    }
    set
    {
        _phone = value;
    }
}
public string address
{
    get
    {
        return _address;
    }
    set
    {
        _address = value;
    }
}
public string post
{
    get
    {
        return _post;
    }
    set
    {
        _post = value;
    }
}
public string gender
{
    get
    {
        return _gender;
    }
    set
    {
        _gender = value;
    }
}


why error is shown in my button coding
C#
j.id=combobox1.text;  "cannot implicitly convert type string to int" 

please help me if an other problem with my coding for update staff also tell me...sorry for my English....
Posted
v2
Comments
Tom Marvolo Riddle 25-Feb-14 0:38am    
Try this
j.id=int.parse(combobox1.text)
Member 10481714 25-Feb-14 0:45am    
thanks...... it is solved
Tom Marvolo Riddle 25-Feb-14 0:57am    
You are welcome
Please mark Solution 1 as answer. :)
Please post it as an answer. :)

1 solution

Try this:
C#
j.id = int.Parse(combobox1.Text);
 
Share this answer
 
v3
Comments
+5. :)

int.TryParse is better than int.Parse.
Tom Marvolo Riddle 25-Feb-14 1:04am    
Thank you Tadit :)
Comment updated. :) Please see.
Tom Marvolo Riddle 25-Feb-14 1:10am    
Thanks for the update Tadit
Welcome. :)

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