This is a migrated thread and some comments may be shown as answers.

Inserting or creating BinaryImage

5 Answers 108 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
Emmanuel
Top achievements
Rank 1
Emmanuel asked on 27 Mar 2012, 05:10 PM
Please can any one there help out? i want to create or insert binaryimage into the database.

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Mar 2012, 11:42 AM
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.
0
Emmanuel
Top achievements
Rank 1
answered on 29 Mar 2012, 12:01 PM
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
0
Princy
Top achievements
Rank 2
answered on 29 Mar 2012, 12:28 PM
Hello,

Try adding the namespace Telerik.Web.UI.

Thanks,
Princy.
0
Emmanuel
Top achievements
Rank 1
answered on 29 Mar 2012, 01:09 PM
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

0
Emmanuel
Top achievements
Rank 1
answered on 29 Mar 2012, 01:38 PM
Please thank you so very much. Is now working perfectly well. I had problem with the database design, thank you and God bless!
Tags
BinaryImage
Asked by
Emmanuel
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Emmanuel
Top achievements
Rank 1
Share this question
or