CrazyEngineers
  • Error in .Net Code

    YashuAsha

    Member

    Updated: Oct 25, 2024
    Views: 972
    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: </Columns>
    Line 39: </asp:GridView>
    Line 40: <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" Line 41: SelectCommand=" Select DocID, DocName, doctype from SaveDoc"></asp:SqlDataSource>
    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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • [Prototype]

    MemberAug 20, 2012

    Add Provider and try.
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register