Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem updating non-text fields with OleDb command
Message
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00694258
Message ID:
00694280
Views:
12
The order of the parameters you add makes a difference when you are using the OleDb provider. You need to reorder the Parameters.Add() to be in the same order as the ? of your UPDATE statement.

>I'm trying to learn ASP.NET by reading the on-line documentation and working through the "walkthroughs." I ran into a problem that I cannot seem to solve in "Walkthrough: Updating Data Using a Database Update Query in Web Forms" (located at ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbwlkwalkthroughupdatingdatausingdatabaseupdatequeryinwebforms.htm)
>
>I followed the instructions closely, except that I used a Visual FoxPro table and OleDb classes instead of SQL Server. The table I used is the Products table in the "testdata" database that ships with VFP 7.0.
>
>The web page has a drop-down list for choosing the product_id, which fills some text boxes from data fields. You edit the text boxes and click the button, which executes the update query.
>
>Here is the command text of the OleDbCommand:
>
>UPDATE products SET eng_name = ?, in_stock = ? WHERE (product_id = ?)
>
>The product_id and eng_name fields are character type; in_stock is numeric. The corresponding OleDbParameterTypes are VarChar and Decimal.
>
>Here is the code in the click method of the button:
>
>// Get the parameter values from the drop-down list and text boxes
>cmdProductsUpdate.Parameters["product_id"].Value = ddlProductID.SelectedItem.Text;
>cmdProductsUpdate.Parameters["eng_name"].Value = txtEnglishName.Text;
>cmdProductsUpdate.Parameters["in_stock"].Value = Convert.ToDecimal(txtInStock.Text);
>// Execute the update query
>oleDbConnection1.Open();
>cmdProductsUpdate.ExecuteNonQuery();
>oleDbConnection1.Close();
>
>I get the following error at the ExecuteNonQuery() line: "Object reference not set to instance of object." It's not clear to me which object reference is at fault.
>
>I tried setting the "in_stock" parameter to a decimal constant (6.0), but that gave the same error. If I omit the in_stock field from the query or replace it with a character field, the update works OK. Also, if I replace the update command with delete and insert commands, the delete works OK but the insert command fails just like the update command.
>
>So I suppose the error has to do with setting the "in_stock" parameter correctly, but I don't see how else to do it. Any suggestions?
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform