GridView ODS and ConvertEmptyStringToNull="false"

A

Andrew Robinson

Woundering if anyone else has had issues with
ConvertEmptyStringToNull="false" ?

I have a custom data class and am passing values to it via an ODS. On my
insert, if the bound text box is empty an empty string or "" is passed to my
data object. On an update, an empty string is converted to null.

The insert does not use the ConvertEmptyStringToNull="false" property and
works the way I was hoping. Setting this property on the update parameters
in the ODS seems to have no affect.

Thanks for any help or ideas. I really don't want to check for nulls in my
data object for each field.

-Andrew

<asp:ObjectDataSource ID="ObjectDataSourceMain" runat="server"
DeleteMethod="Delete" InsertMethod="Insert"
OnInserting="ObjectDataSourceMain_Inserting"
OnSelected="ObjectDataSourceMain_Selected" SelectMethod="Select"
TypeName="MarketStreet.Data.ItemData" UpdateMethod="Update">
<DeleteParameters>
<asp:parameter Name="itemID" Type="Object" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="itemID" Type="Object" />
<asp:parameter Name="itemDescription1" Type="String" />
<asp:parameter Name="itemDescription2" Type="String" />
<asp:parameter Name="itemNote" Type="String"
ConvertEmptyStringToNull="false" />
<asp:parameter Name="itemCode" Type="String" />
<asp:parameter Name="itemPrice" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="itemDescription1" Type="String" />
<asp:parameter Name="itemDescription2" Type="String" />
<asp:parameter Name="itemNote" Type="String" />
<asp:parameter Name="itemCode" Type="String" />
<asp:parameter Name="itemPrice" Type="String" />
</InsertParameters>



[DataObjectMethod(DataObjectMethodType.Update, true)]
public void Update(object itemID, string itemDescription1, string
itemDescription2, string itemNote, string itemCode, string itemPrice)
{

// itemNote is NULL here.

using (SqlConnection cn = new SqlConnection(DataConnection))
using (SqlCommand cm = new SqlCommand("UPDATE Item SET
ItemDescription1=@description1, ItemDescription2=@description2,
ItemNote=@note, ItemCode=@code, ItemPrice=@price WHERE ItemID=@itemID", cn))
{
cn.Open();

cm.CommandType = CommandType.Text;
cm.Parameters.AddWithValue("@itemID", itemID);
cm.Parameters.AddWithValue("@description1",
itemDescription1);
cm.Parameters.AddWithValue("@description2",
itemDescription2);
cm.Parameters.AddWithValue("@note", itemNote);
cm.Parameters.AddWithValue("@code", itemCode);
cm.Parameters.AddWithValue("@price", itemPrice);

cm.ExecuteNonQuery();

cn.Close();
}
}
 
S

Steven Cheng[MSFT]

Hi Andrew,

Thanks you for posting.

As for the ObjectDataSource control parameter's ConvertEmptyStringToNull
setting problem, it is actually due to the separate
ConvertEmptyStringToNull setting for DataSource Control and DataBound
control. For DataSource control(such as objectdatasource), each parameter
has the "ConvertEmptyStringToNull" setting. However, in DataBound control,
like GridView, each DataBoundField(BoundField, TemplateField...) also has
such a "ConvertEmptyStringToNull" property. So for update operation in
GridView, the value first be extracted from GridView's field into
datavalues, then passed to ObjectDataSource, so if we want let the
emptystring be perserved, we should also configure the
"ConvertEmptyStringToNull" on the Field in GridView. e.g:

<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
ConvertEmptyStringToNull="False" />

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top