1

I have a question regarding on update using SqlParameter with class...

In my class EditRecord:

string strScript = "";
strScript = strScript + "UPDATE tbMemberInfo(FirstName,LastName,Age)";
strScript = strScript + "SET (FirstName = @firstname, LastName = @lastname, Age = @age)";
strScript = strScript + "WHERE id=@empID";

SqlCommand cmd;
cmd = new SqlCommand(strScript, Connection);
cmd.CommandTimeout = 0;

cmd.Parameters.AddWithValue("@firstname", _members.Lastname));
cmd.Parameters.AddWithValue("@lastname", _members.FirstName));
cmd.Parameters.AddWithValue("@age", _members.Age));
cmd.Parameters.AddWithValue("@empID", _members.ID));
cmd.ExecuteNonQuery();

Call the class in my Form

Class.clsMemberInfo member = new Class.clsMemberInfo();

member.FirstName = txtFirstName.Text;
member.Lastname = txtLastName.Text;
member.Age = Convert.ToInt32(txtAge.Text);

clsMembers memberInfo = new clsMembers();

memberInfo.Members = member;
memberInfo.EditREcord(cnTestConnection);
MessageBox.Show("Success");

But the data is not updating

8
  • Your query seems off. You need a space after each string. So: "UPDATE tbMemberInfo(FirstName,LastName,Age) " Also, make sure you have opened a connection Commented Dec 6, 2016 at 8:12
  • Your sql maybe upset because between age) and set there is no space, same as there is no space between @age and where. Commented Dec 6, 2016 at 8:12
  • 1
    debug your code, get sql script string before execute it and then run it on Sql Server. you will not be surprised, trust me. Commented Dec 6, 2016 at 8:15
  • Thank you sir for your response but no change at all still I cannot update the data.... :( Commented Dec 6, 2016 at 8:15
  • 1
    @JolanMoski, if you "cannot update data" then there is probably an error occurring, which we need to know. Details are important! Commented Dec 6, 2016 at 8:27

1 Answer 1

1

change sql statement as

string strScript = "UPDATE tbMemberInfo SET FirstName = @firstname, LastName = @lastname, Age = @age WHERE id=@empID";

update syntax is

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

there is no column names given in () next to table name

UPDATE

in your code _members.ID is not set before you call the method

Class.clsMemberInfo member = new Class.clsMemberInfo();

member.FirstName = txtFirstName.Text;
member.Lastname = txtLastName.Text;
member.Age = Convert.ToInt32(txtAge.Text);
member.ID  = ??? //you need to give this value, otherwise your update will not work 
Sign up to request clarification or add additional context in comments.

6 Comments

Care to explain down votes! may be i'm wrong but there is clearly syntax issue with the OPs code
I have already edit my update sql but still cannot update my data
@JolanMoski, if you "cannot update data" then there is probably an error occurring, which we need to know. Details are important!
What additional detail you need to know Sir Peter B?
Your code worked Sir Poovizhi! Now how do i change the value of your sql statements? :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.