Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I have three files one is class filedownload.cs in which i have three functions...GetFileLIST() savefile() and GetaFile().second file is GetFile.aspx and third is form in which code is behind button.using all these files i am uploading a file and showing in gridview and it is working for me.but now i have another form in which i want to use the code for uploading file.but for another form i have to repeat all these code because of query because i have differet table now...
please resolve this problem to make my code short and not to repeat....

filedownload.cs

  public static DataTable GetFileList()
        {
            DataTable dt=new DataTable();
            using (SqlConnection con = new SqlConnection())
            {
                OpenConnection(con);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandTimeout = 0;
                com.CommandText = "SELECT ID, Name, [Content], size FROM Connectivity";
                com.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = com;
                da.Fill(dt);
                con.Close();
            }
               


            
            return dt;

        }
        public static void savefile(string name, string content, int size, byte[] data)
        {
            using (SqlConnection con = new SqlConnection()) 
            {
                OpenConnection(con);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandTimeout = 0;
                string ct = "INSERT INTO Connectivity VALUES (@name,@content,";
                ct = ct + "@size,@data)"; // want different query for diffrent table
                com.CommandText = ct;
                com.CommandType = CommandType.Text;
                com.Parameters.Add("@name", SqlDbType.VarChar, 50);
                com.Parameters.Add("@content", SqlDbType.VarChar, 50);
                com.Parameters.Add("@size", SqlDbType.Int);
                com.Parameters.Add("@data", SqlDbType.VarBinary);

                com.Parameters["@Name"].Value = name;
                com.Parameters["@content"].Value = content;
                com.Parameters["@size"].Value = size;
                com.Parameters["@data"].Value = data;
                com.ExecuteNonQuery();
                con.Close();


            }
        }
        public static DataTable Getafile(int id)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection())
            {
                OpenConnection(con);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandTimeout = 0;
                com.CommandText = "SELECT ID, Name, [Content], size, data FROM Connectivity where ID=@ID";
                com.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter();
                com.Parameters.Add("@ID", SqlDbType.Int);
                com.Parameters["@ID"].Value = id;
                da.SelectCommand = com;
                da.Fill(dt);
                con.Close();


            }
            return dt;
        }

/
Posted
Updated 12-May-13 20:07pm
v3
Comments
Thanks7872 13-May-13 1:57am    
Dont put codes like this. No one would like to go through all this and find the solution. Put the smallest possible snippet,remove unnecessary code.
fak_farrukh 13-May-13 2:08am    
i have removed extra code

Change your functions/procedures/subs/whatever so that they take in either a Stored Procedure name (better) or an SQL string (not quite as good, but will suffice) as a parameter, and use that as the CommandText item...
 
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