Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I write a coding for updating data in database
coding of my class stafag's is
C#
class staffAG
    {
        string _name;
        int _id;
        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;
            }
        }
    }
}

and coding of my class staff is
C#
public void updatestaff(staffAG j)
        {
            cn.Open();
            cmd = new SqlCommand("update staff set name = @name,age = @age,phoneno = @phoneno,address = @address,post = @post,gender = @gender where id = @id", cn);
            cmd.Parameters.AddWithValue("@id", 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.Parameters.AddWithValue("@gender",j.gender);


            cmd.ExecuteNonQuery();
            cn.Close();
            {
                MessageBox.Show("Added");
            }
        }


coding of my update button is

C#
private void button1_Click(object sender, EventArgs e)
        {
            Staff s = new Staff();
            staffAG j = new staffAG();
            j.id = comboBox2.SelectedIndex;
            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");
        }


but when i press the update button this error is displayed
Quote:
The parameterized query '(@id int,@name nvarchar(4000),@age nvarchar(4000),@phoneno nvarc' expects the parameter '@gender', which was not supplied.


please help me..
Posted
Updated 24-Feb-14 7:38am
v2

You did not set up gender property!
C#
Staff s = new Staff();
            staffAG j = new staffAG();
            j.id = comboBox2.SelectedIndex;
            j.name = textBox2.Text;
            j.age = textBox3.Text;
            j.phone = textBox4.Text;
            j.address = textBox5.Text;
            j.post = comboBox1.Text;
            j.gender = ???
            s.updatestaff(j);
 
Share this answer
 
Comments
Member 10481714 24-Feb-14 14:39pm    
thanks friend this problem is solved
but updating is not being done.
private void button1_Click(object sender, EventArgs e)
{
Staff s = new Staff();
staffAG j = new staffAG();
j.id = comboBox1.Text;
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");
}
where i write j.id=combobox1.text; here an error shown "Cannot implicitly convert type 'string' to 'int' "
Maciej Los 24-Feb-14 14:49pm    
id property stores integer value and you set text instead integer. You need to convert text to integer.
Member 10481714 24-Feb-14 23:45pm    
how....?
i am trying to convert it like
j.id=convert.int32(combobox1.text);
it shows error
Member 10481714 24-Feb-14 23:55pm    
Input string was not in a correct format.
Maciej Los 25-Feb-14 1:32am    
In your function public void updatestaff(staffAG j) you are attempting to pass in the parameter @gender by setting it to j.gender but in your function private void button1_Click(object sender, EventArgs e) you have not set j.gender to any value.
 
Share this answer
 
Simply because the value of gender is never set:
C#
private void button1_Click(object sender, EventArgs e)
        {
            Staff s = new Staff();
            staffAG j = new staffAG();
            j.id = comboBox2.SelectedIndex;
            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");
        }

You set the other properties of the staffAG instance, but since you don't set gender, it remains null.
Null passed as a parameter to a column that can't be null, will throw an exception.

BTW: The standards for naming in C# is for an initial uppercase character for properties - it's a good idea to stick with such standards.
 
Share this answer
 
It expects the parameter '@gender', which was not supplied..... :-|

Please check that you have not supplied a value for Gender!!
 
Share this answer
 
Comments
Maciej Los 24-Feb-14 14:42pm    
Please, read the question carefully! Parameter has been successfully set:
cmd.Parameters.AddWithValue("@gender",j.gender);
Rahul Vohra 24-Feb-14 15:45pm    
Please replace your button1_click event handler to following:

private void button1_Click(object sender, EventArgs e)
{
Staff s = new Staff();
staffAG j = new staffAG();
j.id = comboBox2.SelectedIndex;
j.name = textBox2.Text;
j.age = textBox3.Text;
j.phone = textBox4.Text;
j.gender = "Male";

j.address = textBox5.Text;
j.post = comboBox1.Text;
s.updatestaff(j);
MessageBox.Show("Record updated");
}

and see if it works!

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