dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Memory Stream

  1. #1
    Join Date
    Feb 2004
    Posts
    111

    Memory Stream

    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();

  2. #2
    Join Date
    Mar 2005
    Posts
    71
    Check whether 'iTotalByteCount' equals 'iByteCount'.
    If not, you need to seek the stream back to its origin before data read.

    Code:
    stream.Seek(0, SeekOrigin.Begin);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center