3 Answers, 1 is accepted
0

Shinu
Top achievements
Rank 2
answered on 09 Jan 2014, 05:03 AM
Hi Edd,
Please have a look into the following sample code snippet which works fine at my end.
ASPX:
C#:
Thanks,
Shinu.
Please have a look into the following sample code snippet which works fine at my end.
ASPX:
<
telerik:RadAsyncUpload
runat
=
"server"
ID
=
"RadAsyncUpload1"
TargetFolder
=
"~/Uploadimages"
AllowedFileExtensions
=
".jpg"
OnFileUploaded
=
"RadAsyncUpload1_FileUploaded"
>
</
telerik:RadAsyncUpload
>
<
telerik:RadButton
runat
=
"server"
Text
=
"Postback"
ID
=
"RadButton1"
>
</
telerik:RadButton
>
<
telerik:RadButton
ID
=
"RadButton2"
runat
=
"server"
Text
=
"GetImage"
OnClick
=
"RadButton2_Click"
>
</
telerik:RadButton
>
C#:
protected
void
RadAsyncUpload1_FileUploaded(
object
sender, FileUploadedEventArgs e)
{
Stream file = e.File.InputStream;
byte
[] data =
new
byte
[file.Length];
file.Read(data, 0, Convert.ToInt32(file.Length));
String connectionstring = WebConfigurationManager.ConnectionStrings[
"NorthwindConnectionString"
].ConnectionString;
SqlConnection sqlconn =
new
SqlConnection(connectionstring);
try
{
sqlconn.Open();
//inserting the image into db
SqlCommand command =
new
SqlCommand(
"Insert into Pictable (msgid, pic1) Values (6, @pic)"
, sqlconn);
command.Parameters.Add(
"@pic"
, SqlDbType.VarBinary).Value = data;
command.ExecuteNonQuery();
}
finally
{
file.Close();
sqlconn.Close();
}
}
protected
void
RadButton2_Click(
object
sender, EventArgs e)
{
try
{
using
(SqlConnection myConnection =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"NorthwindConnectionString"
].ConnectionString))
{
//accessing image from database
const
string
SQL =
"SELECT [pic1] FROM [Pictable] "
;
SqlCommand myCommand =
new
SqlCommand(SQL, myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
if
(myReader.Read())
{
Response.BinaryWrite((
byte
[])myReader[
"pic1"
]);
}
myReader.Close();
myConnection.Close();
}
}
catch
(Exception ex)
{
Response.Write(ex.ToString());
}
}
Thanks,
Shinu.
0

Edd
Top achievements
Rank 1
answered on 09 Jan 2014, 12:06 PM
Please i used the exact code you provided changing the connectionstrings to suit mine but didnt work.
below are the codes
ASPX
below are the codes
ASPX
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
runat
=
"server"
></
telerik:RadScriptManager
>
<
telerik:RadAsyncUpload
runat
=
"server"
ID
=
"RadAsyncUpload1"
TargetFolder
=
"~/Uploadimages"
AllowedFileExtensions
=
".jpg"
OnFileUploaded
=
"RadAsyncUpload1_FileUploaded"
>
</
telerik:RadAsyncUpload
>
<
telerik:RadButton
runat
=
"server"
Text
=
"Postback"
ID
=
"RadButton1"
>
</
telerik:RadButton
>
<
telerik:RadButton
ID
=
"RadButton2"
runat
=
"server"
Text
=
"GetImage"
OnClick
=
"RadButton2_Click"
>
</
telerik:RadButton
>
</
div
>
</
form
>
</
body
>
C#
protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
Stream file = e.File.InputStream;
byte[] data = new byte[file.Length];
file.Read(data, 0, Convert.ToInt32(file.Length));
String connectionstring = WebConfigurationManager.ConnectionStrings["RealEstateConnectionString1"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(connectionstring);
try
{
sqlconn.Open();
//inserting the image into db
SqlCommand command = new SqlCommand("Insert into Pictable (pic1) Values (@pic)", sqlconn);
command.Parameters.Add("@pic", SqlDbType.VarBinary).Value = data;
command.ExecuteNonQuery();
}
finally
{
file.Close();
sqlconn.Close();
}
}
protected void RadButton2_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["RealEstateConnectionString1"].ConnectionString))
{
//accessing image from database
const string SQL = "SELECT [pic1] FROM [Pictable] ";
SqlCommand myCommand = new SqlCommand(SQL, myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
Response.BinaryWrite((byte[])myReader["pic1"]);
}
myReader.Close();
myConnection.Close();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
Database Script
USE [RealEstate]
GO
/****** Object: Table [dbo].[Pictable] Script Date: 1/9/2014 12:13:34 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Pictable](
[msgid] [int] IDENTITY(1,1) NOT NULL,
[pic1] [image] NULL,
CONSTRAINT [PK_Pictable] PRIMARY KEY CLUSTERED
(
[msgid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Thank you...
0

Shinu
Top achievements
Rank 2
answered on 10 Jan 2014, 10:00 AM
Hi Edd,
Unfortunately I couldn't replicate the issue at my end. The given code was working fine at my end.
Thanks,
Shinu
Unfortunately I couldn't replicate the issue at my end. The given code was working fine at my end.
Thanks,
Shinu