Gridview + ObjectDatasource + UpdateMethod = Input string was not in a correct format

M

M C

Hi,
This is a problem that has been haunting me for days and I've come to a
complete dead-end. I'm using a objectdatasource to select and update a
gridview control. Populating with select works fine but updating gives
the "input string... " error. Any suggestions or links to a working
sample. Let me know if anyone needs any code posted...
Thanks in advance!
MC
 
O

Otis Mukinfus

Hi,
This is a problem that has been haunting me for days and I've come to a
complete dead-end. I'm using a objectdatasource to select and update a
gridview control. Populating with select works fine but updating gives
the "input string... " error. Any suggestions or links to a working
sample. Let me know if anyone needs any code posted...
Thanks in advance!
MC

One of the causes of this Exception description is having the brackets wrong in
a format string. Check you formatting strings for "(" or ")" where they should
be "{" or "}". It sounds as though you might be using a format string to format
the data in an update statement. You could also have the content between the
brackets wrong.

How do I know about this? I make that mistake all the time ;o)

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
M

Manu

Are you updating real or datetime values?

There's a bug in the ObjectDataSource that prevents it from working
properly if the current culture is "compatible" with the
InvariantCulture. For more info take a look here:

http://www.manuelabadia.com/blog/PermaLink,guid,c72852ae-1fdd-4934-a715-f565ceaf21cc.aspx

The only thing you can do to bypass this error if you're using the
ObjectDataSource is to set your current culture to the InvariantCulture
but probably this is not what you want to do as you'll loose your
datetime and real formats.

You can use my ExtendedObjectDataSource that is a component with more
functionality than the ObjectDataSource:

http://www.manuelabadia.com/products/EODS_features.aspx

You have to pay for the complete version but there's a free version
(look at the purchase page) that works like the ObjectDataSource and
doesn't have the "Input string was not in a correct format" error (but
doesn't have caching).

Hope it helps,
Manu.
 
M

MC

I'm only updating int and string values...
Here's a snippet of my code:

[ASPX page]:
<asp:GridView ID="gridViewNews" runat="server"
AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"
DataKeyNames="cid,rid">
<Columns>
<asp:CommandField ShowEditButton="True" >
</asp:CommandField>
<asp:BoundField DataField="cid" HeaderText="CID" ReadOnly="True"
SortExpression="cid" />
<asp:BoundField DataField="rid" HeaderText="RID" ReadOnly="True"
SortExpression="rid" />
<asp:BoundField DataField="controlvocabulary"
HeaderText="Thesaurus" ReadOnly="False"
SortExpression="controlvocabulary" HtmlEncode="false" />
<asp:BoundField DataField="title" HeaderText="Rx Title"
ReadOnly="True" SortExpression="title" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
OldValuesParameterFormatString="original_{}"
SelectMethod="getInvalidItemByNews" UpdateMethod="editNews"
TypeName="Rx">
<UpdateParameters>
<asp:parameter Name="original_cid" Type="Int32" />
<asp:parameter Name="original_rid" Type="Int32" />
<asp:parameter Name="controlvocabulary" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>

[CLASS page with UPDATE method]:
public static void editNews(int original_cid, int original_rid, string
controlvocabulary, string title) {
using (SqlConnection connection = new
SqlConnection(ConfigurationManager.ConnectionStrings["OFHC-RX_RxConnectionString"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("lex_EditNews",
connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@contentid", SqlDbType.Int).Value =
original_cid;
command.Parameters.Add("@revisionid", SqlDbType.Int).Value
= original_rid;
command.Parameters.Add("@controledvocabvalue",
SqlDbType.NVarChar, 1000).Value = controlvocabulary;
int result = 0;
connection.Open();
result = command.ExecuteNonQuery();
connection.Close();
}
}
}
 
M

MC

Here's output error. I really have no clue to what or where this error
resides.
Thanks...

[FormatException: Input string was not in a correct format.]
System.Text.StringBuilder.FormatError() +54
System.Text.StringBuilder.AppendFormat(IFormatProvider provider,
String format, Object[] args) +2834404
System.String.Format(IFormatProvider provider, String format,
Object[] args) +93

System.Web.UI.WebControls.ObjectDataSourceView.MergeDictionaries(ParameterCollection
reference, IDictionary source, IDictionary destination, String
parameterNameFormatString) +229

System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary
keys, IDictionary values, IDictionary oldValues) +2113
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary
values, IDictionary oldValues, DataSourceViewOperationCallback
callback) +78
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row,
Int32 rowIndex, Boolean causesValidation) +1218
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup) +853
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source,
EventArgs e) +87
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args) +35
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source,
EventArgs e) +117
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args) +35
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
+86
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String
eventArgument) +153

System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+172
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+4900
 
M

Manu

I think you should change OldValuesParameterFormatString="original_{}"
to OldValuesParameterFormatString="original_{0}".

Hope it helps,
Manuel Abadia.
 
M

MC

and I tried the culture thing too - it didn't help. Is that something
thats applicable to my problem. As I said earlier, I'm not dealing
with real or datetime values.
MC
 
M

MC

THANK YOU!!!! I finally got some meaningful errors and in a split
second I got it fixed!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top