Inserting or creating BinaryImage

6 posts, 0 answers
  1. Emmanuel
    Emmanuel avatar
    4 posts
    Member since:
    Mar 2012

    Posted 27 Mar 2012 Link to this post

    Please can any one there help out? i want to create or insert binaryimage into the database.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Mar 2012 Link to this post

    Hello,

    Here is the sample that I tried to insert BinaryImage into the database.

    ASPX:
    <telerik:RadTextBox ID="TextBox1" runat="server" Label="CategoryID" ></telerik:RadTextBox><br />
    <telerik:RadTextBox ID="TextBox2" runat="server" Label="CategoryName" ></telerik:RadTextBox><br />
    <telerik:RadTextBox ID="TextBox3" runat="server" Label="Description" ></telerik:RadTextBox><br />
    <telerik:RadAsyncUpload runat="server" ID="AsyncUpload1" AllowedFileExtensions="jpg,jpeg,png,gif" MaxFileSize="1048576" OnValidatingFile="RadAsyncUpload1_ValidatingFile">
    </telerik:RadAsyncUpload>
    <asp:Button ID="Button1" runat="server" Text="upload" onclick="Button1_Click" />

    C#:
    const int MaxTotalBytes = 1048576; // 1 MB
    int totalBytes;
    public bool? IsRadAsyncValid
     {
      get
       {
        if (Session["IsRadAsyncValid"] == null)
         {
          Session["IsRadAsyncValid"] = true;
         }
        return Convert.ToBoolean(Session["IsRadAsyncValid"].ToString());
       }
      set
       {
        Session["IsRadAsyncValid"] = value;
       }
     }
    public static string ConnectionString
     {
      get
       {
        return ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
       }
     }
    protected override void OnLoad(EventArgs e)
     {
      base.OnLoad(e);
      IsRadAsyncValid = null;
     }
    protected void Button1_Click(object sender, EventArgs e)
     {
      if (!IsRadAsyncValid.Value)
       {
        Response.Write("The length of the uploaded file must be less than 1 MB");
        return;
       }
      int CategoryID = int.Parse(TextBox1.Text);
      string CategoryName = TextBox2.Text;
      string Description = TextBox3.Text;
      UploadedFile file = AsyncUpload1.UploadedFiles[0];
      byte[] fileData = new byte[file.InputStream.Length];
      file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);
      using (SqlConnection conn = new SqlConnection(ConnectionString))
       {
        conn.Open();
        SqlCommand cmd = new SqlCommand("INSERT INTO [Categories] ([CategoryID], [CategoryName], [Description], [Picture]) VALUES (@CategoryID,@CategoryName, @Description, @Picture)", conn);
        cmd.Parameters.Add("@CategoryID", SqlDbType.Int, 50, "CategoryID");
        cmd.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 4000, "CategoryName");
        cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 4000, "Description");
        cmd.Parameters.Add("@Picture", SqlDbType.Image);
        cmd.Parameters["@CategoryID"].Value = CategoryID;
        cmd.Parameters["@CategoryName"].Value = CategoryName;
        cmd.Parameters["@Description"].Value = Description;
        cmd.Parameters["@Picture"].Value = fileData;
        cmd.ExecuteScalar();
       }
     }

    Hope it helps,

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Emmanuel
    Emmanuel avatar
    4 posts
    Member since:
    Mar 2012

    Posted 29 Mar 2012 Link to this post

    Thank you so much for help. i tried to run the codes and the system is complaining of this line :
    UploadedFile file = AsyncUpload1.UploadedFiles[0];
    below is the full error :

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0246: The type or namespace name 'UploadedFile' could not be found (are you missing a using directive or an assembly reference?)

    Source Error:

    Line 54:         string CategoryName = TextBox2.Text;
    Line 55:         string Description = TextBox3.Text;
    Line 56:         UploadedFile file = AsyncUpload1.UploadedFiles[0];
    Line 57:         byte[] fileData = new byte[file.InputStream.Length];
    Line 58:         file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);

    Source File: c:\Learning\Telerik Web\RadControlsWebSite1\FirstDemo.aspx.cs    Line: 56
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Mar 2012 Link to this post

    Hello,

    Try adding the namespace Telerik.Web.UI.

    Thanks,
    Princy.
  6. Emmanuel
    Emmanuel avatar
    4 posts
    Member since:
    Mar 2012

    Posted 29 Mar 2012 Link to this post

    Thanks! No error concerning that again. when clicking on the submit button and I got this error:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

  7. Emmanuel
    Emmanuel avatar
    4 posts
    Member since:
    Mar 2012

    Posted 29 Mar 2012 Link to this post

    Please thank you so very much. Is now working perfectly well. I had problem with the database design, thank you and God bless!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017