Solved

Problems updating a datarow in sql

Posted on 2008-10-22
8
237 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

840 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