Hello everyone,
I am sujata.I am going develop web application in that in want save pdf ,doc/docs,xls/xlsx and image file into sql server 2008.when i am going to save Pdf file then it gives above exeption
What I have tried:
Following is my sample code..
1) Design page
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
onclick="btnUpload_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Enter id"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btnRetrive" runat="server" Text="Retrive" Height="31px"
onclick="btnRetrive_Click" style="margin-top: 80px" />
<asp:Button ID="btnUpload0" runat="server" Text="PDFUpload"
Height="21px"
style="margin-top: 64px; margin-bottom: 33px" onclick="btnUpload0_Click" />
2).operation on btnUload_click
protected void btnUpload_Click(object sender, EventArgs e)
{
string filepath = FileUpload1.PostedFile.FileName;
string fileName = Path.GetFileName(filepath);
string ext = Path.GetExtension(fileName);
string contentType = string.Empty;
switch (ext)
{
case ".doc":
contentType = "application/vnd.ms-word";
break;
case ".docx":
contentType = "application/vnd.ms-word";
break;
case ".xls":
contentType = "application/vnd.ms-excel";
break;
case ".xlsx":
contentType = "application/vnd.ms-excel";
break;
case ".jpg":
contentType = "image/jpg";
break;
case ".png":
contentType = "image/png";
break;
case ".gif":
contentType = "image/gif";
break;
case ".pdf":
contentType = "application/pdf";
break;
}
if (contentType != string.Empty)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contenttype = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString)) ;
{
string query = "insert into ImageTable values (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contenttype);
cmd.Parameters.Add("@Data", SqlDbType.VarBinary).Value=bytes;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
Label1.Text = "Upload Succesfully";
con.Close();
}
}
}
when i am going to save image,doc file it works properly,but when i am going to save pdf file some pdf files are save successfully and some raise following exception
String or binary data would be truncated.
The statement has been terminated.</pre>
Please help me to solve this exception