Click here to Skip to main content
15,570,202 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
While giving code for uploading pdf files in asp.net c# it shows 2 error

errors
1.name 'connection' does not exist in the current context
2.The name 'txtFilename'does not exist in the current context

What I have tried:

using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class downloadprescription : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-9RP88PP;Initial Catalog=Project;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label2.Visible = true;
        string filePath = FileUpload1.PostedFile.FileName; // getting the file path of uploaded file  
        string filename1 = Path.GetFileName(filePath); // getting the file name of uploaded file  
        string ext = Path.GetExtension(filename1); // getting the file extension of uploaded file  
        string type = String.Empty;
        if (!FileUpload1.HasFile)
        {
            Label2.Text = "Please Select File"; //if file uploader has no file selected  
        }
        else
            if (FileUpload1.HasFile)
            {
                try
                {
                    switch (ext) // this switch code validate the files which allow to upload only PDF file   
                    {
                        case ".pdf":
                            type = "application/pdf";
                            break;
                    }
                    if (type != String.Empty)
                    {
                        connection();
                        Stream fs = FileUpload1.PostedFile.InputStream;
                        BinaryReader br = new BinaryReader(fs); //reads the binary files  
                        Byte[] bytes = br.ReadBytes((Int32)fs.Length); //counting the file length into bytes  
                       string query = "insert into PDFFiles (Name,type,data)" + " values (@Name, @type, @Data)"; //insert query  
                        SqlCommand cmd = new SqlCommand(query, con);
                        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename1;
                        cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = type;
                        cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
                        cmd.ExecuteNonQuery();
                        Label2.ForeColor = System.Drawing.Color.Green;
                        Label2.Text = "File Uploaded Successfully";
                    }
                    else
                    {
                        Label2.ForeColor = System.Drawing.Color.Red;
                        Label2.Text = "Select Only PDF Files "; // if file is other than speified extension   
                    }
                }
                catch (Exception ex)
                {
                    Label2.Text = "Error: " + ex.Message.ToString();
                }
            }
    }
}
Posted
Updated 6-Nov-22 23:56pm
Comments
Laiju k 7-Nov-22 23:47pm    
If you had copied the code from some another file cross check the file it may contain

connection();

Then you need to copy it to this page.
Or you didn't have the original function then you may need to write one new.
I am just suggesting this because I had done this sort of things in the past.

1 solution

That code doesn't include a method called connection, so the system rightly complains when you try to call it.
In fact, I don't see anything there that even vaguely resembles the word "connection" so we can't fix it for you. Find the class you declared the function in and look at the definition - that should tell you want you need to do to fix it.

The same thing applies to txtFilename - it doesn't exist in that code, so we can't help you find it. In fact, it doesn't appear in that code at all even as a reference, so it's probably a different file and class altogether that is reporting the problem ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900