Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Application
hi all,
 
am developing windows application using c#.net ,
 
in this i have one part to click download and display file in excell sheet,
 
in this portion i have done in asp.net using c# but i need only windows application
 
i tryied my below asp.net code in to windows application but am couldnt get any similar code,
 
so please find out my below asp.net code and give me a windows application code,
 
its very appreciable for help....
 

 
my asp.net code is:
 

 
protected void download_Click(object sender, EventArgs e)
    {
        
        if (DDLCarrier.SelectedItem.Value == "DMSU")
        {
            SqlConnection con = new SqlConnection(ConnectionStr);
            SqlCommand cmd = new SqlCommand("datadownload", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Destinations", SqlDbType.VarChar);
 
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            con.Close();
 
            DataTable dt = Getdata(cmd);
 
            GridView gv1 = new GridView();
            gv1.AllowPaging = false;
            gv1.DataSource = dt;
            gv1.DataBind();
 
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
 
            for (int i = 0; i < gv1.Rows.Count; i++)
            {
                gv1.Rows[i].Attributes.Add("class", "textmode");
 
            }
            gv1.RenderControl(hw);
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
}
 

private DataTable Getdata(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
 
        SqlConnection con = new SqlConnection(ConnectionStr);
        SqlDataAdapter da = new SqlDataAdapter();
 
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        try
        {
            con.Open();
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            da.Dispose();
            con.Dispose();
 
        }
    }
Posted 27-Feb-13 1:03am
Edited 27-Feb-13 1:14am
(no name)17.7K
v2
Comments
boogac at 27-Feb-13 6:14am
   
Convert.ToWindowsApplication(ASP.Net) in the future maybe ?
Balamurugan1989 at 27-Feb-13 7:58am
   
In Windows Form application we can use Download option to download the Excel File.
Balamurugan1989 at 27-Feb-13 8:01am
   
Have you got the solution or could i post it...

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

  Print Answers RSS
0 OriginalGriff 315
1 Jochen Arndt 190
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2014
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