Home > DeveloperSection > Forums > Updating database for specific user
Aaron Douglas
Aaron Douglas

Total Post:20

Posted on    June-19-2013 2:47 AM

 ADO.Net ADO.Net 

 1 Reply(s)
 707  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?

Sumit Kesarwani

Total Post:378

Posted on    June-19-2013 7:56 AM


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.

Don't want to miss updates? Please click the below button!

Follow MindStick