Failed to convert parameter value from a SqlParameter to a Stringhttp://forums.asp.net/t/1546491.aspx/1?Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringWed, 28 Apr 2010 08:39:16 -040015464913781259http://forums.asp.net/p/1546491/3781259.aspx/1?Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringFailed to convert parameter value from a SqlParameter to a String <p>I am using VS2008 with asp.net 3.5 and enterprise library 4.1. I am getting the following error when I try to connect to database.<br style="clear:left"> &quot;Failed to convert parameter value from a SqlParameter to a String&quot;<br style="clear:left"> <br style="clear:left"> Here is my code</p> <p></p> &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;Database db = DatabaseFactory.CreateDatabase(&quot;SQLSRVR&quot;);&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;string myStoredProcedure = &quot;usp_GetPersonContact&quot;;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;SqlParameter[] SQLParameters = new SqlParameter[1];&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;SQLParameters[0] = new SqlParameter(&quot;@ln&quot;, SqlDbType.NVarChar, 50); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;SQLParameters[0].Value = &quot;Smith&quot;;&nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;SQLParameters[0].Direction = ParameterDirection.Input;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;DataSet resultDataSet = null;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;resultDataSet = db.ExecuteDataSet(myStoredProcedure, SQLParameters); &nbsp; &nbsp;&nbsp;&lt;/div&gt; <p></p> <p>Database db = DatabaseFactory.CreateDatabase(&quot;SQLSRVR&quot;);</p> <p>string myStoredProcedure = &quot;usp_GetPersonContact&quot;;</p> <p>SqlParameter[] SQLParameters = new SqlParameter[1];</p> <p>SQLParameters[0] = new SqlParameter(&quot;@lastname&quot;, SqlDbType.NVarChar, 50); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p> <p>SQLParameters[0].Value = &quot;Smith&quot;;&nbsp;</p> <p>SQLParameters[0].Direction = ParameterDirection.Input;</p> <p>DataSet resultDataSet = null;</p> <p>resultDataSet = db.ExecuteDataSet(myStoredProcedure, SQLParameters); &nbsp; &nbsp;&nbsp;</p> <p></p> <p></p> <p><br> </p> <p><span class="Apple-style-span" style="">I already tried conversion but seems nothing is working. Please let me know what i am doing wrong. Thanks.</span></p> 2010-04-13T01:33:47-04:003781344http://forums.asp.net/p/1546491/3781344.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>Seems fine, you sure your issue is not in the sp?&nbsp;</p> 2010-04-13T02:52:12-04:003781354http://forums.asp.net/p/1546491/3781354.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>I think so.&nbsp;</p> <p><br> </p> <p>here is the code for SP</p> <p><br> </p> <p></p> &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;ALTER Procedure [dbo].[usp_GetPersonContact](@ln varchar(50)) &nbsp;&nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;as&nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;select * from Person.Contact where LastName = @ln&lt;/div&gt; <p></p> <p>ALTER Procedure [dbo].[usp_GetPersonContact](@ln varchar(50)) &nbsp;&nbsp;</p> <p>as&nbsp;</p> <p>select * from Person.Contact where LastName = @ln</p> <p></p> <p></p> <p><br> </p> <p><br> </p> 2010-04-13T03:00:11-04:003781356http://forums.asp.net/p/1546491/3781356.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>Parameter name is @lastname not @ln</p> 2010-04-13T03:01:52-04:003782516http://forums.asp.net/p/1546491/3782516.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>As mentioned the&nbsp;parameter name is&nbsp;@In and also the&nbsp;sqldbtype should be&nbsp;varchar, not nvarchar.&nbsp;</p> 2010-04-13T14:57:43-04:003783020http://forums.asp.net/p/1546491/3783020.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>Thanks for your time and help.<br style="clear:left"> <br style="clear:left"> Still is not working. Here is the latest code<br style="clear:left"> <br style="clear:left"> C#&nbsp;<br style="clear:left"> <br style="clear:left"> Database db = DatabaseFactory.CreateDatabase(&quot;SQLSRVR&quot;);<br style="clear:left"> <br style="clear:left"> string myStoredProcedure = &quot;usp_GetPersonContact&quot;;<br style="clear:left"> <br style="clear:left"> SqlParameter[] SQLParameters = new SqlParameter[1];<br style="clear:left"> <br style="clear:left"> SQLParameters[0] = new SqlParameter(&quot;@lastname&quot;, SqlDbType.VarChar, 50, &quot;LastName&quot;);&nbsp;<br style="clear:left"> <br style="clear:left"> SQLParameters[0].Value = &quot;Smith&quot;;&nbsp;<br style="clear:left"> <br style="clear:left"> SQLParameters[0].Direction = ParameterDirection.Input;<br style="clear:left"> <br style="clear:left"> DataSet resultDataSet = null;<br style="clear:left"> <br style="clear:left"> resultDataSet = db.ExecuteDataSet(myStoredProcedure, SQLParameters);<br style="clear:left"> <br style="clear:left"> Storeprocedure<br style="clear:left"> <br style="clear:left"> ALTER Procedure [dbo].[usp_GetPersonContact]@lastname varchar(50)&nbsp;<br style="clear:left"> as&nbsp;<br style="clear:left"> select * from Person.Contact where LastName = @lastname</p> 2010-04-13T19:19:31-04:003793567http://forums.asp.net/p/1546491/3793567.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>Any help folks? My requirement is to pass stored procedure with SQL parameters together </p> 2010-04-20T06:29:24-04:003793602http://forums.asp.net/p/1546491/3793602.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>Database db = DatabaseFactory.CreateDatabase(&quot;SQLSRVR&quot;);<br style="clear:left"> <br style="clear:left"> string myStoredProcedure = &quot;usp_GetPersonContact&quot;;<br style="clear:left"> <br style="clear:left"> SqlParameter[] SQLParameters = new SqlParameter[1];</p> <p><br style="clear:left"> <br style="clear:left"> <strong>SQLParameters[0] = new SqlParameter(&quot;@lastname&quot;, &quot;Ur LastName Value&quot;);&nbsp;<br style="clear:left"> </strong><br style="clear:left"> <br style="clear:left"> <br style="clear:left"> DataSet resultDataSet = null;<br style="clear:left"> <br style="clear:left"> resultDataSet = db.ExecuteDataSet(myStoredProcedure, SQLParameters);<br style="clear:left"> &nbsp;</p> 2010-04-20T06:46:17-04:003793746http://forums.asp.net/p/1546491/3793746.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>Hi,</p> <p>try this</p> <p><strong>First add references</strong></p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2"></p> <p>&nbsp;&nbsp; using </font></font><font size="2">System.Data.Common;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </p> <p>&nbsp;&nbsp; using </font></font><font size="2">Microsoft.Practices.EnterpriseLibrary.Data;</font></p> <p><font size="2"></font>&nbsp;</p> <p><font size="2"><strong>Then your code will be:</strong></font></p> <p></p> <p>&nbsp;&nbsp;&nbsp; Database db = DatabaseFactory.CreateDatabase(&quot;SQLSRVR&quot;);</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;string myStoredProcedure = &quot;usp_GetPersonContact&quot;;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;DbCommand cmd = db.GetStoredProcCommand(myStoredProcedure);</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;SqlParameter param = new SqlParameter(&quot;lastname&quot;,SqlDbType.NVarChar);<br> &nbsp;&nbsp;&nbsp;&nbsp;param.Value = &quot;Smith&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;param.Direction = ParameterDirection.Input;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;cmd.Parameters.Add(param);</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;DataSet resultDataSet = null;</p> <p>&nbsp;&nbsp;&nbsp; resultDataSet = db.ExecuteDataSet(cmd);</p> 2010-04-20T08:08:27-04:003807469http://forums.asp.net/p/1546491/3807469.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p>Thanks your time and help Sathish Kumar and Omkar lale.&nbsp;</p> <p><br> </p> <p>Sathish kumar -- i tried your approach but no luck. I am getting the same error. thanks.</p> <p><br> </p> <p>Omkar lale - Currently, I am implemented your code only. But the issue in this approach is we need to </p> <p>add all sql parameters into command object and execute it.&nbsp;</p> <p><br> </p> 2010-04-27T22:20:24-04:003808253http://forums.asp.net/p/1546491/3808253.aspx/1?Re+Failed+to+convert+parameter+value+from+a+SqlParameter+to+a+StringRe: Failed to convert parameter value from a SqlParameter to a String <p></p> <pre style="padding-right:5px; padding-left:5px; font-weight:normal; padding-bottom:5px; margin:0px; word-break:break-all; padding-top:5px; font-style:normal; font-family:Consolas,Courier,monospace; word-wrap:break-word">go through this link </pre> <pre style="padding-right:5px; padding-left:5px; font-weight:normal; padding-bottom:5px; margin:0px; word-break:break-all; padding-top:5px; font-style:normal; font-family:Consolas,Courier,monospace; word-wrap:break-word"><a href="http://msdn.microsoft.com/en-us/library/dd140044.aspx">http://msdn.microsoft.com/en-us/library/dd140044.aspx</a></pre> <pre style="padding-right:5px; padding-left:5px; font-weight:normal; padding-bottom:5px; margin:0px; word-break:break-all; padding-top:5px; font-style:normal; font-family:Consolas,Courier,monospace; word-wrap:break-word">&nbsp;</pre> <p><span></span><span></span></p> 2010-04-28T08:39:16-04:00