Note that there are some explanatory texts on larger screens.

plurals
  1. POData source controls and parameter type conversion
    text
    copied!<p><br /><br /></p> <p>Q1 - In the code example below runtime converts a value between two incompatible types:</p> <pre><code> &lt;SelectParameters&gt; &lt;asp:Parameter Name="City" Type="Int32" /&gt; &lt;/SelectParameters&gt; protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e) {e.Command.Parameters["@City"].Value = "100";} </code></pre> <p>Exception I got:</p> <p>“Conversion failed when converting the nvarchar value 'Seattle' to data type int.” <br /><br /></p> <p>A) The above exception suggests that runtime did manage to convert a value of type String into a value of type Int32, and thus the exception happened on SqlServer?! <br /><br /></p> <p>B) Since String and Int32 are incompatible types, why did runtime perform conversion from String to Int32 in the first place?</p> <p>Doesn’t the fact that we’re dealing with incompatible type make runtime “realize” that app most likely has a bug, similary to the way compiler “realizes” that it’s dealing ( in the code below ) with two incompatible types:</p> <pre><code>String s = ”something”; int i = (int)s; //error </code></pre> <p><br /><br /></p> <p>Q2:</p> <p>A) public void GetEmployee( int EmployeeID );</p> <pre><code>&lt;asp:ObjectDataSource SelectMethod=”GetEmployee” …&gt; &lt;SelectParameters&gt; &lt;asp:ControlParameter Name = ”EmployeeID” ...&gt; &lt;/SelectParameters&gt; </code></pre> <p><br /></p> <p>If for whatever reason EmployeeID parameter is NULL, ObjectDataSource will convert Null to zero and passed it as argument to GetEmployee() method.</p> <p>Why does runtime make such a conversion? Wouldn't throwing an exception made more sense? <br /><br /></p> <p>B) “Use the ConvertEmptyStringToNull property to specify whether an empty string value is automatically converted to null when the data field is updated in the data source.” <br /> I don’t quite understand the usefulness of this property. Why would empty string indicate that we want null to be inserted into source’s data field? I’d assume that this data field is of type String? Then why not also have ConvertZeroInt32ToNull etc? <br /><br /></p> <p>Q3:</p> <pre><code>&lt;asp:SqlDataSource ID="sourceEmployees" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="&lt;%$ ConnectionStrings:Northwind %&gt;" SelectCommand="SELECT EmployeeID, FirstName,LastName, Title, City FROM Employees WHERE City=@City"&gt; &lt;SelectParameters&gt; &lt;asp:Parameter Name="City"/&gt; &lt;/SelectParameters&gt; &lt;/asp:SqlDataSource&gt; </code></pre> <p>A) I assume that when you don’t specify of which type Parameter instance “City” is, it is automatically of type Object, which means it can later be assigned value of any type. Thus if “City” is later ( say inside SqlDataSource2_Selecting() event handler ) assigned a value of a wrong type, this wrong assignment will only be detected on Sql server, and not before ( of course Sql server will report that error back to web server )? <br /><br /></p> <p>B) If we create a SqlParameter instance of type NVarChar(20) and want to pass this parameter to a stored procedure, will Ado.net pass to a stored procedure just the value of this parameter, or will it also somehow inform the procedure the exact type of this parameter ( which is NVarChar(20))? <br /><br /></p> <p>thanx </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload