UPDATING DATABASE FOR SPECIFIC USER

Aaron Douglas

Total Post:20

Points:140
Posted by  Aaron Douglas
 1023  View(s)
Ratings:
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);
    connection.Open();
    cmd.ExecuteNonQuery();  
}

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. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Updating database for specific user

    Hi, 


    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!