Friday, January 7, 2011

I came across this error when I was coding a file that updated a table using SqlDataSource with a Stored Procedure.

""Procedure or function "  .... "has too many arguments specified"

Googled for the keywords above and I found this suggestion to my problem from phynix at forums.asp.net.

Adding the code solved my problem totally! ^.^

Thank you phynix!


Add an event handle for your SqlDataSource's Updating event:
for example:
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
 protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
 {
  DbParameterCollection CmdParams = e.Command.Parameters;
  ParameterCollection UpdParams = ((SqlDataSourceView)sender).UpdateParameters;
  Hashtable ht = new Hashtable();
  foreach (Parameter UpdParam in UpdParams)
      ht.Add(UpdParam.Name, true);
  for (int i = 0; i < CmdParams.Count; i++)
  {
      if (!ht.Contains(CmdParams[i].ParameterName.Substring(1)))
          CmdParams.Remove(CmdParams[i--]);
  }
 }

Wednesday, January 5, 2011

Steps to generate an Excel file from a GridView

 

Assuming you have a databound GridView called GridView1 whose contents you want to export to an Excel file named “ExcelFile.xls” upon clicking a button with ID “ButtonExport”, there are two basic steps what you need to do.

Step 1: Add the code to the event handler for ButtonExport click event as follows:

protected void ButtonExport_Click(object sender, EventArgs e)
{
   Response.Clear();
   Response.Buffer = true;
   Response.AddHeader("content-disposition",
                      "attachment;filename=ExcelFile.xls");

   Response.Charset = "";
   Response.ContentType = "application/vnd.ms-excel";

   StringWriter sw = new StringWriter();
   HtmlTextWriter hw = new HtmlTextWriter(sw);

   GridView1.AllowPaging = false;
   GridView1.DataBind();
   GridView1.RenderControl(hw);

   Response.Output.Write(sw.ToString());
   Response.Flush();
   Response.End();
} //end ButtonExport_Click

Step 2: Add the VerifyRenderingInServerForm method

You will need to add this method in order for the code in Step 1 to work.

public override void VerifyRenderingInServerForm(Control control)
{
       return;
}