Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 26th, 2009, 11:15 AM
Authorized User
 
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts
Default Exporting database to client

Hi,

I want to give my users the opportunity to download part of my database (some fields are excluded for privacy reasons). After downloading they should be able to process the data as they please (could be importing into Excel or a statiscal package such SSPS or STATA). I've been searching the web for 5 days now, but I have no idea in what direction I should look in order to find a solution.
1. How can I write a file on a client computer?
2. Should I try to export SQL files or should it be text files?
3. Can I use C# or is it better to use another language?

thanks

Hans Koolschijn
 
Old March 26th, 2009, 11:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Hans,

Is this related to the book Beginning ASP.NET 3.5? If not, please consider posting in a different category as more users will see you post.

There are many ways to do what you want, from simple to complex.

Simple would be: Query the data and while iterating over the result set, Response.Write the output (you may need to set the content type to text)

Or you could export to Excel. Search Google for Excel export ASP.NET for some ideas.

You could also use tools like Sql Server Integration Services which is designed for these tasks. However, that requires you to use SQL Server.

Finally, you could search Google for FileHelpers, a .NET class library for exporting objects to CSV. The project seems dead now, but the 2.0 version works reasonably well...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 26th, 2009, 06:32 PM
Authorized User
 
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Hi Imar,

I guess you're right, I'd better post in the PRO category. However, I build my website by reading and rereading your book and googling.
As for my question, using Response.write might generate a lot of code and won't look very neat (or am I wrong).
I found a script for exporting to Excel, which doesn't look too complicated.
I also found some scripts using BulkCopy. But that seems to require a destination string. Do my users have to install SQL server in that case?

thanks

Hans
 
Old March 27th, 2009, 04:38 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, bcp (or the .NET classes that enable the same functionality) work between instances of SQL Server only.

Why would outputting to the response be a lot of code? It can be as simple as:

Code:
 
while (myReader.read())
{
  Response.Write(string.Format("{0};{1}\r\n", myReader.GetString(0), myReader.GetString(1)));
}
Note: I am not saying it's the best thing to do, but it certainly is an option for simple exports.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 30th, 2009, 03:08 PM
Authorized User
 
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Hi Imar,

the following code works:

Code:
    protected void Button1_Click(object sender, EventArgs e)
    {

string queryString = "SELECT achternaam, voornamen FROM Personen;";

      string connectionString = WebConfigurationManager.ConnectionStrings["ScaDatabaseConnectionString"].ConnectionString;

      using (SqlConnection connection = new SqlConnection(connectionString))
      {
        SqlCommand command = new SqlCommand(queryString, connection);
        connection.Open();

        SqlDataReader myReader = command.ExecuteReader();

        // Call Read before accessing data.

        while (myReader.Read())
        {
          StringBuilder myString = new StringBuilder("");
          for (int field = 0; field < myReader.FieldCount; field++)
          {
            
            if (!myReader.IsDBNull(field))
            {
              myString.Append(myReader[field]);
              myString.Append(";");
            }
            else
            {
              myString.Append(string.Empty);
              myString.Append(";");
            }
            
          }
          myString.Append("\r\n");
          Response.Write(myString);
        }
        // Call Close when done reading.
        myReader.Close();
      }
      Response.ContentType = "text/richtext";
Response.AppendHeader("Content-Disposition", "attachment; filename=TextFile.txt");
Response.End(); }
but:
1. The file only contains values and not the field names.
2. I need to add a check for the variable type, which can be int or bool as well. I assume I can use GetType for this.

I also tried the code below. I assume bcp copies field names as well and the code is shorter (and maybe runs faster).
When running the code however. I get a syntax error near the '.' in 'cmd.ExecuteNonQuery();' at the end of the code.
Do you have any idea about what's wrong here? Or is this code a bad idea anyway?

Code:
    protected void Button1_Click(object sender, EventArgs e)
    {
string connectionString = WebConfigurationManager.ConnectionStrings["ScaDatabaseConnectionString"].ConnectionString;
          string sCommandText;

          sCommandText = "exec xp_cmdShell 'bcp.exe'" + "..Personen"+ " out " +
                           "C:\\Test\\personen.txt" + " -c -q -U " + " -P " + "-t  ";

          SqlConnection conn = new SqlConnection(connectionString);
          conn.Open();

          SqlCommand cmd = new SqlCommand();
          cmd.Connection = conn;
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = sCommandText;

           cmd.ExecuteNonQuery();  
    }
thanks,

Hans
 
Old March 30th, 2009, 03:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can use GetName to get the names of the columns and ouput them to StringBuilder: http://msdn.microsoft.com/en-us/libr...r.getname.aspx

Alternaticely, since you already know the fields (as they are in your SQL statement), you can directly add them.

With regards to xp_cmdShell: I don't think you want that. There are some serious security issues associated with incorrect use of the procedure and security configuration: http://www.google.com/search?hl=en&q...+security+hack

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 1st, 2009, 03:53 PM
Authorized User
 
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Hi Imar,

GetName works fine.
The only problem left is that when using

Response.AppendHeader("Content-Disposition", "attachment; filename=" + myFile);

I can only send just one file to the user, while I have 10. Is there a way to send the user multiple files or a folder containing those files?

thanks,

Hans
 
Old April 1st, 2009, 05:51 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Hans,

Because of how HTTP works, you can't send multiple files from the same HTTP response.

You could zip them at the server and send the zip file as a single download.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 2nd, 2009, 03:49 PM
Authorized User
 
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Dear Imar,

You really helped me out! The program works fine now. Using a zip file would have been the best solution I guess, but since I have only a few users and since they will only download one or a few files on a regular basis, I decided to use a radiobuttonlist and let them download the files one by one.

For anyone interested, I'll copy the code below.
Code:
// Part of the aspx file

    <tr><td>
      <asp:RadioButtonList ID="RadioButtonList1" runat="server">
      </asp:RadioButtonList>
      <asp:RadioButtonList ID="rblExport" runat="server">
      <asp:ListItem Value="personen">Personen Algemeen en Genetica</asp:ListItem>
      <asp:ListItem Value="celbiologie">Celbiologie</asp:ListItem>
      </asp:RadioButtonList></td></tr>

    <tr><td><asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" 
      Text="Exporteer" /></td></tr>

// Code behind

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

      foreach (ListItem item in rblExport.Items)
      {
        if (item.Selected == true)
        {
          string connectionString = WebConfigurationManager.ConnectionStrings["ScaDatabaseConnectionString"].ConnectionString;
          string filename = "";
          using (SqlConnection connection = new SqlConnection(connectionString))
          {
            string queryString = "";

            if (item.Value == "personen")
            {
              queryString = "SELECT achternaam, voornamen, vaderId FROM Personen;";
              filename = "Personen.txt";
            }
            else if (item.Value == "celbiologie")
            {
              queryString = "SELECT * FROM Celbiologie;";
              filename = "Celbiologie.txt";
            }

            SqlCommand command = new SqlCommand(queryString, connection);
            connection.Open();

            SqlDataReader myReader = command.ExecuteReader();

            StringBuilder myColString = new StringBuilder("");
            for (int field = 0; field < myReader.FieldCount; field++)
            {
              myColString.Append(myReader.GetName(field)); myColString.Append(";");
            }
            myColString.Append("\r\n");
            Response.Write(myColString);

            while (myReader.Read())
            {
              StringBuilder myString = new StringBuilder("");
              for (int field = 0; field < myReader.FieldCount; field++)
              {

                if (!myReader.IsDBNull(field))
                {
                  myString.Append(myReader[field]);
                  myString.Append(";");
                }
                else
                {
                  myString.Append(string.Empty);
                  myString.Append(";");
                }

              }
              myString.Append("\r\n");
              Response.Write(myString);
            }

            // Call Close when done reading.
            myReader.Close();
          }
 
          Response.ContentType = "text/richtext";
          Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
          Response.End();
        }
      }
    }
thanks a lot

Hans
The Following User Says Thank You to hans For This Useful Post:
Imar (April 2nd, 2009)
 
Old April 2nd, 2009, 03:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Great. Glad it's working and thanks for posting a follow up...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
importing and exporting database with multi-langua lenseere Oracle 0 November 30th, 2007 07:34 PM
importing and exporting database in multi-language lenseere General .NET 0 November 29th, 2007 04:47 PM
Exporting Table to other database Scootterp Access VBA 4 September 5th, 2006 06:18 PM
exporting access database to sql server Sheena SQL Server 2000 2 December 29th, 2004 04:06 AM
Exporting access database to Excel .xls Squall Leonhart Classic ASP Databases 5 December 2nd, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.