Solved

Problems updating a datarow in sql

Posted on 2008-10-22
8
234 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 49

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 49

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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 49

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 49

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now