Click here to Skip to main content
11,629,535 members (63,192 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 12-May-13 19:34pm
Edited 12-May-13 20:07pm
v3
Comments
Róhãn Lëuvä at 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.
Member 7909184 at 13-May-13 2:08am
   
i have removed extra code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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...
  Permalink  

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web04 | 2.8.150723.1 | Last Updated 13 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100