Monday, May 24, 2010

IMPORTING EXCEL FILE INTO ASP.NET GRIDVIEW

protected void Button1_Click(object sender, EventArgs e)
{
string xConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("ExcelImport.xls") + ";" +
"Extended Properties=Excel 8.0;";

OleDbConnection objXConn = new OleDbConnection(xConnStr);
string sql = "SELECT * FROM [Salary$]";
OleDbCommand cmd = objXConn.CreateCommand();
cmd.CommandText = sql;
OleDbDataAdapter adpt = new OleDbDataAdapter();
adpt.SelectCommand = cmd;
DataSet ds = new DataSet();
adpt.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();


}

Tuesday, April 6, 2010

Sys.WebForms.PageRequestManagerParserErrorException

I was still getting "Sys.WebForms.PageRequestManagerParserErrorException" error despite trying suggestions in the various website. To place a dummy session did not work as well as I had to put many variables into session and then redirect it to page i.e. I required to use Response.Redirect anyways (Server.Transfer did not work as well). The session variables had to be created after a button "btnFinish" was clicked. I tried following line of code
and actually it worked.

ASP.NET AJAX and Sys.Webforms.PageRequestManagerServerErrorException



Using ASP.NET AJAX extensively in my latest project I've been sporadically running into the Sys.WebForms.PageRequestManagerParserErrorException. It got to the point that I was contemplating ripping out ASP.NET AJAX completely until this known issue had been ironed out. The various causes for this error are mentioned many different places, but for some samples, go here, here, and here.


Quoting from Eilon Lipton's blog posting, this particular exception is very common and can be caused by any one of the following:


  1. Calls to Response.Write():
    By calling Response.Write() directly you are bypassing the normal rendering mechanism of ASP.NET controls. The bits you write are going straight out to the client without further processing (well, mostly...). This means that UpdatePanel can't encode the data in its special format.

  2. Response filters:
    Similar to Response.Write(), response filters can change the rendering in such a way that the UpdatePanel won't know.

  3. HttpModules:
    Again, the same deal as Response.Write() and response filters.

  4. Server trace is enabled:
    If I were going to implement trace again, I'd do it differently. Trace is effectively written out using Response.Write(), and as such messes up the special format that we use for UpdatePanel.

  5. Calls to Server.Transfer():
    Unfortunately, there's no way to detect that Server.Transfer() was called. This means that UpdatePanel can't do anything intelligent when someone calls Server.Transfer(). The response sent back to the client is the HTML markup from the page to which you transferred. Since its HTML and not the special format, it can't be parsed, and you get the error.


The problem was I wasn't doing any of the above (who uses Response.Write in an ASP.NET application these days?) and I was still sporadically encountering the error - a show stopping error I might add. An error that is popped up in a javascript warning box completely undecipherable to the end user leaving an empty/useless/castrated UpdatePanel in its wake. This of course leaves the end user feeling likewise empty/useless/castrated (to say nothing of the developer).


This post here indicates that there is a problem with the RoleMangerModule or any time you set a cookie to the response in an AJAX callback, which can only be solved by doing one of the following:



  1. Disable caching of cookies in the RoleManager. (yuck)

  2. Handle the Application's Error event to clear the error from the Context selectively (eek).

  3. Disable EventValidation in the web form.
    <%@ Page Language="C#" EnableEventValidation="false" %>
    (gulp)


None of the above are entirely reasonable solutions (especially the last two), and the worst part was that my test page was just a simple contact page that did not change/set roles or cookies, or response.write, or set anything in the session, and wasn't receiving any Unicode character input, or even requiring a user to be logged in, or writing anything to the trace, or anything beyond the basics. And still it blew up. But only occasionally.


In order to faithfully reproduce the error, I finally determined that it must have something to do with sessions as it would only occur if the app pool had recycled and all browser windows had been closed. So, based on one of the comments in one of the above posts, even though I'm not touching session on one of the problem pages, I tried a hack in one of the problem page's Page_Load:


Session["FixAJAXSysBug"] = true;


And lo and behold, we're good to go! So even though I am not using Session on the problem page it must be attempting to set the initial session cookie using the Update Panel callback. So the solution is to make sure the initial session is set before any Update Panel callback takes place. How this got through into production is beyond me.


So if you're sporadically encountering the Sys.Webforms.PageRequestManagerServerErrorException, it could be for any of the above reasons or the fact that your dog/cat/stuffed teddy bear is sitting too close to your monitor. But give the last one a try in every page utilizing AJAX if you're using sessions in your application.


UPDATE: If the problem pages aren't even using session, just turn session off for the page:


<%@ Page EnableSessionState="false" ... %>

Or better yet, set it in your base class to always be off, and turn it on for the pages where you need it on.


UPDATE II: Further developments.





Wednesday, May 21, 2008

How to add a textbox inside a gridview

First add a gridview in your page and go to the source view of your page and find the gridview inside which you want to add the textbox and you can insert a code as below (the one in bold blue color) and it will add the textbox into the gridview when it is bound to the database object.

-------------------------------------------------------------
Just below it is the code with which you can access these individual textbox in the different rows.
-------------------------------------------------------------------------------------
for (int j = 0; j < GridView1.Rows.Count; j++)
{
Control cnt;
cnt = GridView1.Rows[j].Cells[3].Controls[1];
cnt = GridView1.Rows[j].FindControl("txtValue");
TextBox txtBx = (TextBox)cnt;
//write your code here to maniupulate the content of textbox



}

Monday, May 12, 2008

How to pass login parameter (username, password) of database to crystal report from code

using CrystalDecisions.Reporting;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
private void displayReport()
{
try
{
CrystalReportViewer1.EnableDatabaseLogonPrompt = false;
ConnectionInfo connInfo = new ConnectionInfo();
TableLogOnInfo tblLogInfo = new TableLogOnInfo();
TableLogOnInfos tblLogInfos = new TableLogOnInfos();
connInfo.UserID = "usrName";
connInfo.Password = "passwd";
connInfo.ServerName = "database";
tblLogInfo.ConnectionInfo = connInfo;
tblLogInfos.Add(tblLogInfo);
CrystalReportViewer1.LogOnInfo = tblLogInfos;
CrystalReportViewer1.EnableParameterPrompt = false;
CrystalReportSource1.ReportDocument.FileName=Server.MapPath ("Reports\\crystNewMain.rpt");
}
Catch
{
}
}

Saturday, May 10, 2008

How to pass parameter to crystal report from ASP.NET

Suppose there is a crystal report which expects parameter id and section. Here’s the code that shows how to pass the parameter through ASP.net
Before this please do not forget to use following namespaces

using CrystalDecisions.Reporting;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;

private void displayReport(number id,string section)
{
try
{

CrystalReportViewer1.EnableParameterPrompt = false;
ParameterFields pFields = new ParameterFields();
ParameterField pField1 = new ParameterField();
ParameterField pField2 = new ParameterField();
ParameterDiscreteValue pDisValue1 = new ParameterDiscreteValue();
ParameterDiscreteValue pDisValue2 = new ParameterDiscreteValue();
pField1.Name = "id";
pField2.Name = "section";
pDisValue1.Value = id;
pField1.CurrentValues.Add(pDisValue1);
pFields.Add(pField1);
pDisValue2.Value = section;
pField2.CurrentValues.Add(pDisValue2);
pFields.Add(pField2);
CrystalReportViewer1.ParameterFieldInfo = pFields;
}
Catch
{
}
}

Friday, April 18, 2008

System.Data.OracleClient requires Oracle client software version 8.1.7 or greater

Cause
Security permissions were not properly set when the Oracle 9i Release 2 client was installed on Windows with NTFS. The result of this is that content of the ORACLE_HOME directory is not visible to Authenticated Users on the machine; this again causes an error while the System.Data.OracleClient is communicating with the Oracle Connectivity software from an ASP.NET using Authenticated User privileges.
Solution
To fix the problem you have to give the Authenticated Users group privilege to the Oracle Home directory.

  1. Log on to Windwos as a user with Administrator privileges.
  2. Start Window Explorer and navigate to the ORACLE_HOME folder.
  3. Choose properties on the ORACLE_HOME folder.
  4. Click the "Security" tab of the "Properties" window.
  5. Click on "Authenticated Users" item in the "Name" list.
  6. Uncheck the "Read and Execute" box in the "Permissions" list under the "Allow" column.
  7. Re-check the "Read and Execute" box under the "Allow" column
  8. Click the "Advanced" button and in the "Permission Entries" verify that "Authenticated Users" are listed with permission = "Read & Execute", and Apply To = "This folder, subfolders and files". If not, edit that line and make sure that "Apply To" drop-down box is set to "This folder, subfolders and files". This should already be set properly but it is important that you verify it.
  9. Click the "Ok" button until you close out all of the security properties windows. The cursor may present the hour glass for a few seconds as it applies the permissions you just changed to all subfolders and files.
  10. Reboot, to assure that the changes have taken effect.