Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error in SQL procedure using CLR

Author  Topic 

mavershang
Posting Yak Master

111 Posts

Posted - 2010-06-10 : 19:08:51
Hi there. I am a newbie to CLR, so my question might be naive.

Here is the code
------------------------------
Dictionary<double,double> cdfTable = new Dictionary<double, double>();

using (SqlConnection conn_aln = new SqlConnection("context connection = true"))

{
string sql_aln;

SqlCommand cmd_aln = new SqlCommand();

cmd_aln.CommandType = CommandType.Text;

cmd_aln.Connection = conn_aln;

SqlDataReader reader;

conn_aln.Open();

sql_aln = " select x, CumulativeDensity_toPositiveInfinite from [crwdb].[dbo].[NormalDistributionTable] ";

cmd_aln.CommandText = sql_aln;

reader = cmd_aln.ExecuteReader();

using (reader)

{

while (reader.Read())

{

cdfTable.Add(reader.GetDouble(0), reader.GetDouble(1));

}

}
}

SqlContext.Pipe.SendResultsStart(outputRecTest3);

foreach (KeyValuePair<double, double> pair in cdfTable)

{



outputRecTest3.SetDouble(0, (double)pair.Key);

outputRecTest3.SetDouble(1, (double)pair.Value);

SqlContext.Pipe.SendResultsRow(outputRecTest3);

}

SqlContext.Pipe.SendResultsEnd();
------------------------------------------

However, error keep pumping out saying "Specified cast is not valid"
System.InvalidCastException:

at Microsoft.SqlServer.Server.ValueUtilsSmi.ThrowIfInvalidSetterAccess(SmiMetaData metaData, ExtendedClrTypeCode setterTypeCode)

at Microsoft.SqlServer.Server.SqlDataRecord.SetDouble(Int32 ordinal, Double value)

at StoredProcedures.sp_BackGroundMI_Shuffling_CLR(String TnameIn, Int32 AlnID, Int32 RepeatShuffling)

.

The two columns read from database are sqltype float, so I thought it should be right if I use type double in my code.

Anyone could give me some suggestion?

Thanks in advance.
   

- Advertisement -