Advertise with us

Updating database for specific user

Posted by  Aaron Douglas
 1517  View(s)
Rate this:
Hi Expert, 

I'm trying to updata my sql database for a specific name, and the following code is not working. Any ideas why?

Here is what I am doing on the button click:

string person = listBox2.SelectedItem.ToString();
string memberStatus = "platinum";
string conn = "Data Source=.;Initial Catalog=myDB;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(conn))
    SqlCommand cmd = new SqlCommand("UPDATE Client Set Role = @Status  WHERE Name = @name");
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;
    cmd.Parameters.AddWithValue("@Status", memberStatus);
    cmd.Parameters.AddWithValue("@name", person);

When I check the database, the Client's Role has not been updated. And when I debug, the value of person is "System.Data.DataRowView" Why is that? How can i fix it?

  1. Re: Updating database for specific user


    The reason the value is System.Data.DataRowView is because you bound your list to a DataView. And that's cool. But we need to take that into consideration:

    string person = ((DataRowView)listBox2.SelectedItem)["Name"] as string;

    Where Name is the name of the field that contains the person's name. I'm making an assumption that the field is named Name. This will fix your query.