hi
can anyone tell me.how can i convert the file into byte array.
Thank you
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Krishna GaradPosted Jan 29, 2011, 4:12 AM
In database create procedure
Create Procedure Sp_InsertFile
(
@resume VarBinary(max)
)
As
Begin
Insert Into Sample(Resume)Values(@resume)
End
Now in .cs file
if(FileUpload1.Hasfile)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
string strconn = @"data source=system-7\sqlexpress;initial
catalog=jobs;uid=sa;pwd=as";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand("Sp_InsertFile", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@resume", SqlDbType.VarBinary);
cmd.Parameters[0].Value = buffer;
cmd.ExecuteNonQuery();
Response.Write("");
cmd.Dispose();
conn.Close();
}
And while Executing put a break point and check exact what kind of error is it. Because both code work fine.
fds vgvf dfgvPosted Apr 23, 2013, 12:13 AM
Bharat BhushanPosted Jan 29, 2011, 7:04 AM
http://useofaspdotnet.blogspot.com/2011/01/how-to-encrypt-password-and-store-it-in.html
Suthish NairPosted Jan 29, 2011, 5:41 AM
Dont copy paste the code, will not helps you. try to understand how the code/procedure works.
Check below all links, if this didnt helps you so we cant.
Link1 Link2 Link3 Link4
krithika muthukrishnanPosted Jan 29, 2011, 5:24 AM
Krishna GaradPosted Jan 29, 2011, 4:32 AM
krithika muthukrishnanPosted Jan 29, 2011, 4:28 AM
but i dont know why the previous codings didn't give best answer.
krithika muthukrishnanPosted Jan 29, 2011, 12:23 AM
Krishna GaradPosted Jan 28, 2011, 11:53 PM
krithika muthukrishnanPosted Jan 28, 2011, 11:50 PM
here i attached my code behind page.
Suthish NairPosted Jan 28, 2011, 12:12 PM
Krishna GaradPosted Jan 28, 2011, 7:55 AM
else also write like this don't modify anything in this.
if (FileUpload1.HasFile)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
Response.Write(buffer);
string strconn = @"data source=system-7\sqlexpress;
catalog=jobs;uid=sa;pwd=as";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = ("insert into sample(resume) values('" + buffer + "')");
cmd.ExecuteNonQuery();
Response.Write("");
cmd.Dispose();
conn.Close();
}
just copy and paste it's working. if it not worked again just restart your SQL Server and visual studio.
krithika muthukrishnanPosted Jan 28, 2011, 7:40 AM
public byte[] FileToByteArray()
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
Response.Write(buffer);
string strconn = @"data source=system-7\sqlexpress;initial
catalog=jobs;uid=sa;pwd=as";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string query = "insert into sample(resume) values('" + buffer + "')";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
Response.Write("");
cmd.Dispose();
conn.Close();
return buffer;
}
Krishna GaradPosted Jan 28, 2011, 7:10 AM
public byte[] FileToByteArray(string FileName)
{
BinaryReader br = new BinaryReader(FileUpload1.
byte[] buffer = br.ReadBytes(FileUpload1.
Response.Write(buffer);
string strconn = @"data source=system-7\sqlexpress;
//string query = "select * from register";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand(); //Here you dont want to pass you select query
cmd.Connection = conn;
cmd.CommandText = ("insert into sample(columnname) values(" +buffer + ")");
//add columname in place of columnname.
cmd.ExecuteNonQuery();//Error occur in this line
Response.Write("");
cmd.Dispose();
conn.Close();
return buffer;
}
I tested it in my db also it's now working. let me know if not. and if fileupload hasfile then only it will create byte[].
krithika muthukrishnanPosted Jan 28, 2011, 6:52 AM
Krishna GaradPosted Jan 28, 2011, 6:46 AM
public byte[] FileToByteArray(string FileName)
{
BinaryReader br = new BinaryReader(FileUpload1.
byte[] buffer = br.ReadBytes(FileUpload1.
Response.Write(buffer);
string strconn = @"data source=system-7\sqlexpress;
string query = "select * from register";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = conn;
cmd.CommandText = ("insert into sample(columnname) values(" +buffer + ")");
//add columname in place of columnname.
cmd.ExecuteNonQuery();//Error occur in this line
Response.Write("");
cmd.Dispose();
conn.Close();
return buffer;
}
krithika muthukrishnanPosted Jan 28, 2011, 6:36 AM
public byte[] FileToByteArray(string FileName)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
Response.Write(buffer);
SqlParameter[] Param=new SqlParameter[1];
Param[0] = new SqlParameter("@image", SqlDbType.Image);
Param[0].Value = buffer;
string strconn = @"data source=system-7\sqlexpress;initial catalog=jobs;uid=sa;pwd=as";
string query = "select * from register";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = conn;
cmd.CommandText = ("insert into sample values('" + Param + "')");
cmd.ExecuteNonQuery();//Error occur in this line
Response.Write("");
cmd.Dispose();
conn.Close();
return buffer;
}
Krishna GaradPosted Jan 28, 2011, 6:30 AM
krithika muthukrishnanPosted Jan 28, 2011, 6:28 AM
Implicit conversion from datatype varchar to varbinary is not allowed error will occur in cmd.executenonquery() line.
Krishna GaradPosted Jan 28, 2011, 6:20 AM
Krishna GaradPosted Jan 28, 2011, 6:03 AM
SqlParameter[] Param=new SqlParameter[1];//this is the error
Param[0] = new SqlParameter("@image", SqlDbType.VarBinary);
Param[0].Value = buffer
increase the size of SqlParameter
krithika muthukrishnanPosted Jan 28, 2011, 5:58 AM
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);Response.Write(buffer);
SqlParameter[] Param=new SqlParameter[0];
Param[0] = new SqlParameter("@image", SqlDbType.Image);
Param[0].Value = buffer;//here the error will occur index was outside the range
Krishna GaradPosted Jan 28, 2011, 5:54 AM
krithika muthukrishnanPosted Jan 28, 2011, 5:51 AM
ERror: Index was outside the bounds of the array.
Krishna GaradPosted Jan 28, 2011, 5:11 AM
In aspx.cs where you are uploading event happens
byte[] buffer;
if(FileUpload1.HasFile)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
// Insert this byte array now to database as buffer.
}
In Insert Method
Insert File (byte[] filearray)
{
SqlParameter[] Param=new SqlParameter[0];
Param[0] = new SqlParameter("@image", SqlDbType.Image);
Param[0].Value = filearray;
//further insert to database attach this parameter to your command.
}
krithika muthukrishnanPosted Jan 28, 2011, 4:59 AM
in sql table i create the field for store byte array in a data type varbinary(max).when i insert the bytearray buffer to the sql table ,I have an error like
Error : Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
Suthish NairPosted Jan 28, 2011, 4:49 AM
Krishna GaradPosted Jan 28, 2011, 4:35 AM
byte[] buffer;
if(FileUpload1.HasFile)
{
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
// Insert this byte array now to database as buffer.
}
krithika muthukrishnanPosted Jan 28, 2011, 4:31 AM
Krishna GaradPosted Jan 28, 2011, 4:21 AM
krithika muthukrishnanPosted Jan 28, 2011, 4:17 AM
when i use this coding,i have an error in this line ,i choose the file from D:\.but it will show the error like could not find file in C:\.how to solve this.
coding :
FileStream fs = new FileStream(FileName, FileMode.Open,FileAccess.Read);
Error :
Could not find file 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\time.doc'.
Krishna GaradPosted Jan 28, 2011, 1:16 AM
krithika muthukrishnanPosted Jan 28, 2011, 1:06 AM
i wish to know how to convert the bytearray to a file,
thank you
Krishna GaradPosted Jan 24, 2011, 12:28 AM
BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] buffer = br.ReadBytes(FileUpload1.PostedFile.ContentLength);