I have a thrid party control that generates a stream object. After the stream is generated, I need to insert that information into a SQL Server database table.

My problem is that the stream is not being inserted correctly and when I attempt to pull that same data from the database, nothing is there.

The code I'm using the insert the stream data into the database is below. If anyone has any idea as to where I'm going wrong, I would love to hear from you.

Many Thanks!

System.IO.Stream stream = new MemoryStream();

try
{
this.MapComponentsManager1.SaveState(stream, false);

//Get length of stream (lengt of file) in bytes
int iByteCount = (int)stream.Length;
byte[] MapStream = new byte[iByteCount];
int iTotalByteCount = stream.Read(MapStream, 0, iByteCount);

//Open a connection
SqlConnection SqlCn = new SqlConnection(ConfigurationSettings.AppSettings["dev_connection"].ToString());
SqlCn.Open();

//Create the command
SqlCommand SqlCmd = new SqlCommand("dbo.sp_SaveMap", SqlCn);
SqlCmd.CommandType = CommandType.StoredProcedure;

//Add the parameters
SqlCmd.Parameters.Add("@MapServiceName", SqlDbType.VarChar, Session["CurrentMapService"].ToString().Length);
SqlCmd.Parameters["@MapServiceName"].Value = Session["CurrentMapService"].ToString();

SqlCmd.Parameters.Add("@SessionID", SqlDbType.VarChar, Session.SessionID.Length);
SqlCmd.Parameters["@SessionID"].Value = Session.SessionID.ToString();

SqlCmd.Parameters.Add("@userid", SqlDbType.Int);
SqlCmd.Parameters["@userid"].Value = mUserID;

SqlCmd.Parameters.Add("@Public", SqlDbType.Bit);
SqlCmd.Parameters["@Public"].Value = 1;

SqlCmd.Parameters.Add("@MapImage", SqlDbType.Image);
SqlCmd.Parameters["@MapImage"].Value = MapStream;

//Execute the procedure and return data to the data reader
SqlCmd.ExecuteNonQuery();

SqlCmd.Dispose();
SqlCn.Close();
SqlCn.Dispose();