Error in .Net Code

here is my code to save and retrieve the word/excel/image/pdf files to sql database and display them in grid view using handler there is no error when i compile it but when i execute the code the following error occurs.
I have created SaveDoc table to store files in it with DocName,Type,DocData columns

The connection name 'ConnectionString' was not found in the applications configuration or the connection string is empty.

Line 38:
Line 39:
Line 40: Line 41: SelectCommand=" Select DocID, DocName, doctype from SaveDoc">
Line 42:
protected void btnUpload_Click(object sender, EventArgs e)
{

if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string documentType = string.Empty;

switch (fileExtension)
{
case ".pdf":
documentType = "application/pdf";
break;
case ".xls":
documentType = "application/vnd.ms-excel";
break;
case ".xlsx":
documentType = "application/vnd.ms-excel";
break;
case ".doc":
documentType = "application/vnd.ms-word";
break;
case ".docx":
documentType = "application/vnd.ms-word";
break;
case ".gif":
documentType = "image/gif";
break;
case ".png":
documentType = "image/png";
break;
case ".jpg":
documentType = "image/jpg";
break;
}
int fileSize = FileUpload1.PostedFile.ContentLength;
byte[] documentBinary = new byte[fileSize];
FileUpload1.PostedFile.InputStream.Read(documentBinary, 0, fileSize);


SqlConnection con = new SqlConnection(@"Data Source=localhost\sqlexpress;Initial Catalog=klausDB;Trusted_Connection=true");
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO SaveDoc(DocName,Type,DocData)" + " VALUES (@DocName,@Type,@DocData)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlParameter DocName = new SqlParameter("@DocName", SqlDbType.VarChar, 50);
DocName.Value = fileName.ToString();
cmd.Parameters.Add(DocName);

SqlParameter doctype = new SqlParameter("@doctype", SqlDbType.VarChar, 50);
doctype.Value = documentType.ToString();
cmd.Parameters.Add(doctype);

SqlParameter uploadedDocument = new SqlParameter("@DocData", SqlDbType.Binary, fileSize);
uploadedDocument.Value = documentBinary;
cmd.Parameters.Add(uploadedDocument);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
lblmessage.Text = "File saved to database";
GridView1.DataBind();
}
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
string fileName = string.Empty;
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
int documentID = Convert.ToInt32(GridView1.DataKeys[index].Value);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT DocName,DocData FROM SaveDoc WHERE DocID = " + documentID, con);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
while (dReader.Read())
{

fileName = dReader["DocName"].ToString();
byte[] documentBinary = (byte[])dReader["DocData"];
FileStream fStream = new FileStream(Server.MapPath("Docs") + @"\" + fileName, FileMode.Create);
fStream.Write(documentBinary, 0, documentBinary.Length);
fStream.Close();
fStream.Dispose();
}
con.Close();
Response.Redirect(@"Docs\" + fileName);
}


Please help me solve this error.

Replies

  • [Prototype]
    [Prototype]
    Add Provider and try.
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"

You are reading an archived discussion.

Related Posts

Respected sir/medam Iam venkataramana completed my B.E(mechanical) in osmania university in 2012 passed out.Iam in intrested in mechanical software designs, i have learnt the softwares also,so i want my own...
This is exactly how I feel when my Internet connection is down. Just wondering if I'm the only one. What about others?

hie

hie guys hope i will benefit a lot from you
Those who're into Arduino would love to know that there's a smaller and cheaper alternative being developed, called Digispark. I wrote about it on VoiCE. Check it out - https://www.crazyengineers.com/digis...ble-usb-development-production-platform-2667/
Use TeamWox Groupware for free to manage your team of up to 10 users. Features:​- Collaboration & Voice communication (IP PBX phone system)​- HRM, Team management & Payroll​- CRM, Service...