Solved

Problems updating a datarow in sql

Posted on 2008-10-22
8
236 Views
Last Modified: 2012-05-05
I am attempting to update a datarow in a sql database with a selected item in a ddl.  I am using the following code:

 Dim connectionstring As String = ("my connection")
        Dim query As String = ("Update properties Set available='@available' where ID='@ID'")
        Dim conn As New SqlConnection(my connection)
        Dim cmd As New SqlCommand(query, conn)
        cmd.Parameters.AddWithValue("@available", DropDownList4.SelectedItem)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
Basically, I want the person to be able to update that particular record.  When I run this application, this is the error I am getting:

System.ArgumentException: No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type.
0
Comment
Question by:smitty68521
  • 4
  • 4
8 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 22783419
try:

Dim query As String = ("Update properties Set available=@available where ID=@ID")

instead?

also, you need provide 2 parameters for the command object.
0
 
LVL 1

Author Comment

by:smitty68521
ID: 22783456
I have tried that and I still get the same error. I see what you are saying as far as adding two parameters for the command object. However, I am not sure how to set the command parameter for the ID when it is simply a querystring I use from a previous page that dispays the data in the gridview.

I appreciate your help so far.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 22783632
try:

        Dim ID As String = ""
        If String.IsNullOrEmpty(Request.QueryString("ID")) = False Then
            ID = Request.QueryString("ID").ToString()
        Else
             'Or redirect or terminate process
        End If

'...

Dim connectionstring As String = ("my connection")
        Dim query As String = ("Update properties Set available=@available where ID=@ID")
        Dim conn As New SqlConnection(my connection)
        Dim cmd As New SqlCommand(query, conn)

        cmd.Parameters.AddWithValue("@available", DropDownList4.SelectedItem)
        cmd.Parameters.AddWithValue("@ID", ID)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 1

Author Comment

by:smitty68521
ID: 22783742
This didn't work either. It doesn't give any errors this time but it also doesn't update the database. I will keep researching. I tried a request.querystring earlier but it didn't work either. Thanks for your help so far.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 22783767
it works for me, perhaps you can post a more complete scripts here?
0
 
LVL 1

Author Comment

by:smitty68521
ID: 22783829
More complete? What I had in my first post was it. This is what is causing the error. It stops at "execute nonquery" I don't know what more I could provide you with. I am not sure what is going on with this. I have had projects similar to this and got them to work just fine. I appreciate your help so far though.
0
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 22783950
try change:

DropDownList4.SelectedItem

to:

DropDownList4.SelectedItem.Value

?
0
 
LVL 1

Author Closing Comment

by:smitty68521
ID: 31509063
It worked! Thanks for your help. Great Expert.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question