Hello,
I'm using SQL sder er 2008 Standard/
I am trying to use a script component to do a lookup, but when I execute the package (in debugging mode), In get an error screen.
Part of the error description is in Dutch, but it says "Object reference not set to an instance of an object "
![]()
More detailed, from the debug output screen:
Error: 0xC0047062 at Data Flow Task, Script Component 1 [1518]: System.NullReferenceException: Object reference not set to an instance of an object .
at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.HandleUserException(Exception e)
at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.PreExecute()
at Microsoft.SqlServer.Dts.Pipeline.ManagedComponentHost.HostPreExecute(IDTSManagedComponentWrapper100 wrapper)
Error: 0xC004701A at Data Flow Task, SSIS.Pipeline: component "Script Component 1" (1518) failed the pre-execute phase and returned error code 0x80004003.
This is the script:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data.SqlClient;
using System.Collections.Generic;
public
struct RangeItem :
IComparable<RangeItem>,
IComparable<DateTime>
{
public Int32 Key;
public Int32 Value;
public RangeItem(Int32 key,
Int32 value)
{
Key = key;
Value = value;
}
#region IComparable Members
public int CompareTo(RangeItem other)
{
return Key.CompareTo(other.Key);
}
public int CompareTo(DateTime other)
{
return Key.CompareTo(other);
}
#endregion
}
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public
class ScriptMain :
UserComponent
{
// Variables
SqlConnection connection =
null;
Dictionary<String,
List<RangeItem>> Cache =
new Dictionary<String,
List<RangeItem>>();
const
string query = "select WerkID, MedewerkerID, Werk_eind_KEY from DimMedewerker";
public
override void PreExecute()
{
base.PreExecute();
connection = (SqlConnection)this.Connections.Connection.AcquireConnection(null);
if (this.Variables.PreCache)
{
FillCache();
}
}
private void FillCache()
{
FillCache(null);
}
private List<RangeItem> FillCache(string key)
{
string sql = query;
if (!string.IsNullOrEmpty(key))
{
sql += string.Format(" where MedewerkerID = '{0}'", key);
}
sql += " Order By MedewerkerID, Werk_eind_KEY";
SqlCommand command =
new SqlCommand(sql, connection);
SqlDataReader reader = command.ExecuteReader();
List<RangeItem> times =
null;
try
{
Int32 lastKey = 0;
Int32 endKey = 0;
while (reader.Read())
{
Int32 WerkID = reader.GetInt32(0);
Int32 MedewerkerID = reader.GetInt32(1);
Int32 Werk_eind_KEY;
if (reader.IsDBNull(2))
{
Werk_eind_KEY = Int32.MaxValue;
}
else
{
Werk_eind_KEY = reader.GetInt32(2);
}
if (lastKey != MedewerkerID)
{
if (!string.IsNullOrEmpty(Convert.ToString(lastKey)))
{
// cache everything up until now
times.Add(new
RangeItem(Int32.MaxValue, endKey));
Cache.Add(Convert.ToString(lastKey), times);
}
lastKey = MedewerkerID;
times = new List<RangeItem>();
}
if (Werk_eind_KEY <
Int32.MaxValue)
{
times.Add(new
RangeItem(Werk_eind_KEY, WerkID));
}
else
{
endKey = WerkID;
}
}
times.Add(new
RangeItem(Int32.MaxValue, endKey));
if (!string.IsNullOrEmpty(Convert.ToString(lastKey)) && times !=
null && times.Count > 0)
{
try
{
Cache.Add(Convert.ToString(lastKey), times);
}
catch (Exception e)
{
throw new
Exception(lastKey + " " + times[0].Key +
" " + times[0].Value, e);
}
}
}
finally
{
reader.Close();
}
return times;
}
public override
void PostExecute()
{
base.PostExecute();
}
public override
void Input0_ProcessInputRow(Input0Buffer Row)
{
Int32 MedewerkerID = Row.MedewerkerID;
List<RangeItem> ranges =
null;
if (!Cache.TryGetValue(Convert.ToString(MedewerkerID),
out ranges))
{
// We don't have this value yet
ranges = FillCache(Convert.ToString(MedewerkerID));
}
if (ranges == null)
{
throw new
Exception("Couldn't find value for MedewerkerID " + MedewerkerID);
}
int index = 0;
if (ranges.Count > 1)
{
index = ranges.BinarySearch(new
RangeItem(Row.ZiekDagKEY, 0));
if (index < 0)
{
index = ~index;
}
}
try
{
Row.WerkID = ranges[index].Value;
}
catch (Exception e)
{
throw new
Exception(string.Format("size: {0} index: {1} key: {2}", ranges.Count, index, Row.MedewerkerID), e);
}
}
}
What am I doing wrong?
Regards, Hennie