Thursday, December 24, 2009

[No relevant source lines]:Compiler Error Message: CS0433

Hi, Sorry again for a long communication gap. I was indulged into many things other than programming. Last week I came across the "Compiler Error Message: CS0433", "[No relevant source lines]". I was on vacation at my village without access to the internet when one of my friends told me about the error. I got the feel that the the specified error can be due to duplicate assemblies. I asked further about the hosting parameters and environment, he told me that initially they had precompiled deployment and now it's with the source.

If you have 2 assemblies (ddl files) within the same web application bin folder named _Default. This could happen if another ddl had a web page named default.aspx whose codebehind was compiled in that ddl but you (or someone else) replaced the default.aspx whose codebehind was in another assembly.

So the error is due to version conflict of dlls. Clean the bin folder old version of dlls.

My friend removed the bin folder and recompiled the solution and then published. The error disappeared.

Wednesday, July 8, 2009

Running 32 bit .net application on 64 bit operating system

IIS 6.0 supports both the 32-bit mode and the 64-bit mode. However IIS 6.0 does not support running both modes at the same time on a 64-bit version of Windows. ASP.NET 1.1 runs only in 32-bit mode. ASP.NET 2.0 runs in 32-bit mode or in 64-bit mode. Therefore, if you want to run ASP.NET 1.1 and ASP.NET 2.0 at the same time, you must run IIS in 32-bit mode.


ASP.NET 1.1, 32-bit version
To run the 32-bit version of ASP.NET 1.1, follow these steps:
1. Click Start, click Run, type cmd, and then click OK.
2. Type the following command to enable the 32-bit mode:
cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1
3. Type the following command to install the version of ASP.NET 1.1 and to install the script maps at the IIS root and under:
%SYSTEMROOT%\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i
4. Make sure that the status of ASP.NET version 1.1.4322 is set to Allowed in the Web service extension list in Internet Information Services Manager.


ASP.NET 2.0, 32-bit version
To run the 32-bit version of ASP.NET 2.0, follow these steps:1. Click Start, click Run, type cmd, and then click OK.
2. Type the following command to enable the 32-bit mode:
cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1
3. Type the following command to install the version of ASP.NET 2.0 (32-bit) and to install the script maps at the IIS root and under:
%SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i
4. Make sure that the status of ASP.NET version 2.0.50727 (32-bit) is set to Allowed in the Web service extension list in Internet Information Services Manager.


ASP.NET 2.0, 64-bit version
To run the 64-bit version of ASP.NET 2.0, follow these steps:1. Click Start, click Run, type cmd, and then click OK.
2. Type the following command to disable the 32-bit mode:
cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 0
3. Type the following command to install the version of ASP.NET 2.0 and to install the script maps at the IIS root and under:
%SYSTEMROOT%\Microsoft.NET\Framework64\v2.0.50727\aspnet_regiis.exe -i
4. Make sure that the status of ASP.NET version 2.0.50727 is set to Allowed in the Web service extension list in Internet Information Services Manager.
Note The build version of ASP.NET 2.0 may differ depending on what the currently released build version is. These steps are for build version 2.0.50727.

Thursday, March 20, 2008

Hey I am Back!

Sorry for a long communication gap. I was busy on a project based on GIS and Intelligence (a project other than ASP.NET technologies). It was quite challenging for me because it was a new experience at all. I'll discuss it later and will be taking the previous misson forward. Again I apolozise for inconvience.
Cheers!
Arun

Wednesday, April 25, 2007

Loading excel sheet (Workbook) data to SQL database using ASP.NET C#

This will describe about loading data from excel sheet (workbook) to SQL database from an asp.net application. It will also provide solutions to all the queries related to exporting excel to SQL, using bulk copy, loading excel data to SQL database etc.

There are many ways to load excel sheet (workbook) data to SQL database. Here I am describing about using bulk copy and data reader. Before proceeding further I would like to mention that we need to insert (define) name to rows and columns of excel sheet that contains the actual data. The first row will be treated as names of columns and rest as data.


Now loading excel sheet (workbook) data to SQL database using C#. Let we have Book1.xls excel file that has our data and we need to load this data to tbl_Emp_details table of our SQL database. Let the excel sheet has five columns i.e. name, sex, age, address and qualification. Also our SQL database has same columns along with ID column (primary key). Please note that if you are using bulk copy command then the column sequence of excel sheet and SQL data table must be same. If you are using primary key column (Identity column) then make this column the last column of the table because the bulk copy client will insert first column data of data reader into the first column of SQL data table and so on. If you have defined the first column as Identity column (primary key) then the data of first column of excel sheet will be skipped and the data of remaining columns will be inserted in the sequence of data reader columns. Before loading the excel file data do these things:

1. Select all rows and columns of excel sheet and click on ‘Insert’ menu and select ‘Names’ then ‘Define’.
2. In the popup window define (type) name of table (e.g. myExcelTable) in ‘Names in workBook’ and click ‘OK’.


We define name (Insere Name) is because the selected rows and columns will be treated as a table and we can execute select command on it. Now the defined name will be the name of the table and the first row of excel sheet will be treated as the names of columns and remaining rows as data. If you do not define name then you will get the error as “The Microsoft Jet database engine could not find the object 'myExcelTable'. Make sure the object exists and that you spell its name and the path name correctly.”

Define connection string in webconfig file for SQL database (In example it is for express database)

<connectionStrings> <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=DataDirectory\ASPNETDB.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" /> </connectionStrings>

Now add references of Oledb and SqlClient to the code behind file.

using System.Data.OleDb;
using System.Data.SqlClient;


Now define a string variable to store connection string for Oledb connection and assign it the proper string. Define another string variable and assign sql connection string (connection string defined in webconfig file) to it. Now initialize a new Oledb connection using Oledb Connection string and open it. Define Oledb command and initialize it with select command. The table name in select command will be the name that we had defined (Inserted) by selecting rows and columns of excel sheet. Note: First row of excel sheet will be treated as the names of columns and we will select these columns only in our select command.
Now using data reader and bulk copy command we will insert data into SQL database.


//drive conniction string from webconfig file
string SQLconnStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//define connectionstring and excel file name for Oledb database engine
string OledbConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:/Book1.xls;" + "Extended Properties=Excel 8.0;";
//Oledb connection
OleDbConnection myOLEDBConn = new OleDbConnection(OledbConStr);
try
{

myOLEDBConn.Open();
//select data (rows) from excel sheet (workbook)
OleDbCommand myOLEDBCmnd = new OleDbCommand("select name, sex, age, address, qualification from myExcelTable", myOLEDBConn);

using (OleDbDataReader dbRdr = myOLEDBCmnd.ExecuteReader())
{
//save to sql database
using (SqlBulkCopy mysqlblkcpy = new SqlBulkCopy(SQLconnStr))
{
//define destination table name
mysqlblkcpy.DestinationTableName = "tbl_Emp_Details";
//define batch size in order to make operation easy
mysqlblkcpy.BatchSize = 500;
mysqlblkcpy.WriteToServer(dbRdr);
}
}


}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}



If we want more reliable method to load excel data to SQL database then we should use simple insert command instead of bulk copy command.

Note: If you receive the error: "Could not find installable ISAM." It means that your Oledb Connection string is not correct. Either you have misspell something or
Extended Properties=Excel 8.0; version is incorrect. Try the version 11.0 in place of 8.0 or correct spellings.

What to do when error like “ System.Data.SqlClient.SqlException: Failed to update database "C:\....\ASPNETDB.MDF" because the database is read-only"

What to do when error comes like “Exception Details: System.Data.SqlClient.SqlException: Failed to update database "C:\INETPUB\WWWROOT\....\APP_DATA\ASPNETDB.MDF" because the database is read-only”
or
“Exception Details: System.Data.SqlClient.SqlException: Failed to save to database "C:\INETPUB\WWWROOT\....\APP_DATA\ASPNETDB.MDF" because the database is read-only”

“Database is read-only” types of errors are common errors encountered when hosting the website on IIS with MS SQL Express database. These types of errors may occur when you have the NTFS file system and ASPNET user account does not have proper access permissions. If you do not provide write permissions to ASPNET user account to the hosted directory this error will occur. Simply add ASPNET user account to permission list to the directory and assign write permissions to this account. There are many ways to assign permissions and overcome this problem.

First do not host (place) your website to wwwroot directory. Either save it to the root of primary drive (C:\) or host it some where else because sometimes default access permissions do not provide ASPNET user account the write permissions. If you are hosting your website to wwwroot directory then make sure that ASPNET user account has write permission to App_Data Directory or .MDF file. You can check this by right clicking on App_Data directory and then selecting security tab. If you are able to see ASPNET user account in “Group or user names” then click on it and make sure that ‘write check box’ is checked (selected) in “permissions for ASPNET”. If it is not checked (selected) then check(select) it. If you do not find ASPNET user account into “Groups or user names” then add it and give it write permissions.

Second if you are hosting your website to any directory other than wwwroot then give ASPNET user account write permissions to website directory or App_Data directory as follow:

1. Right clicking on App_Data directory and then selecting security tab.
2. If you are able to see ASPNET user account in “Group or user names” then click on it and make sure that ‘write check box’ is checked (selected) in “permissions for ASPNET”. If it is not checked (selected) then check(select) it.
3. If you ado not find ASPNET user account into “Groups or user names” then click on ‘Add’.
4. In ‘Select Users or Groups’ windows click on ‘Advance’.
5. Now click on ‘Find Now’.
6. Here you will see the list of users. Now select ASPNET and click on ‘Ok’ to close third window and click again on ‘Ok’ to close second window (Select Users or Group).
7. Now click on ASPNET user account and check (select) write check box and click on ‘Apply’ and then ‘Ok’.

Now you have enough permission to your ASPNET user account to write or update Express Database. You can run your website now.

Third, you can place your website in shared folder but the access (write) permissions to ASPNET user account are required.

Thus you can overcome these types of errors.
If you still have problem, Please feel free to seek help.

To turn on the security tab in folder properties please follow the process:

. Click 'Tools' menu of explorer and select 'Folder Options'
. Select 'View' tab and uncheck (remove) the option 'Use simple file sharing (Recommended)' from the 'Advanced Options'. This option is the last in the list.

Thursday, March 15, 2007

Generating word document dynamically with header, footer and page border using ASP.NET (c#)

Since Microsoft Word has good support for HTML, you can generate word document (customized) from your asp.net application very easily. First define a string type variable, strDocBody, which will contain the body of word document. Now add the page HTML to strDocBody as follow:

strDocBody = "<!-- /* Page HTML or WordML */><html " +
"xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word'" +
"xmlns='http://www.w3.org/TR/REC-html40'>" +
"<head>" +
"<title>Dynamic Generated Document</title>"
;

This will define xmlns properties (standreds) and title of document. Now set the document properties like layout view, zoom etc.

strDocBody = strDocBody + "<!-- /* Page layout(view) Definitions */>" +
"<!--[if gte mso 9]>" +
"<xml>" +
"<w:WordDocument>" +
"<w:View>Print</w:View>" +
"<w:Zoom>100</w:Zoom>" +
"<w:DoNotOptimizeForBrowser/>" +
"</w:WordDocument>" +
"</xml>" +
"<![endif]>";

Here "<w:View>Print</w:View>" represents the Print Layout View and "<w:Zoom>100</w:Zoom>" represents the page zoom. Now add the style section. This section will contain the page style properties like page size, header id, footer id, page border, margins, font family etc.

strDocBody = strDocBody +"<!-- /* Page Style Definitions */>" +
"<style> @page Section1 " +
" {size:8.5in 11.0in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt; " +
"margin:0.75in 0.50in 0.75in 0.50in ; " +
"mso-header-margin:.5in; " +
"mso-footer-margin:.5in; mso-paper-source:0;}" +
"div.Section1" +
" {page:Section1;}" + "p.MsoFooter, li.MsoFooter, div.MsoFooter{margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}" +
"p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}" +
"-->" +
"</style>" +
"</head>";


The size of page is defined as size:8.5in 11.0in; and mso-first-footer:ff1; defines the first page footer if you select the option Different first page from menu File > Page Setup > Layout tab. mso-footer: f1; defines footer for all pages. The same way you can define first page header and header for all pages as mso-header: h1;. Other header and footer properties are defined in MsoFooter, MsoHeaderer classes as shown in sample code. border:solid navy 2.25pt; defines the page border and padding:24.0pt 24.0pt 24.0pt 24.0pt; defines page border position.
Now add the actual body of the document and header and footer.

strDocBody = strDocBody + "<!-- /* Actual document body */>" +
"<body lang=EN-US style='tab-interval:.5in'>" +
"<div class=Section1>" +
"<h1>This is my Heading</h1>" +
"<h2>This is my Sub Heading</h2>" +
"<p style='color:navy;'> This is blue text</p>" +
"<p style='font-weight:bold; color:green;'><u> This is green bold underlined text </u></p>" +
"<p style='color:red'><I>" +
DateTime.Now + "</I></p>" +
//my image
"<img img width=217 height=162 id='myImg' src='C:/WINDOWS/Web/Wallpaper/Autumn.jpg'>" ;




strDocBody = strDocBody + "<!-- /* Header and footer */>" +
"<!--[if supportFields]>" +
"<div style='mso-element:header' id=h1><p class=MsoHeader><span style='mso-tab-count:4'></span><span style='mso-field-code: PAGE '></span> </p></div>" +
"<div style='mso-element:footer' id=f1> " +
"<p class=MsoFooter style='border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0in;mso-padding-alt:0in 0in 1.0pt 0in'</p> " +
"Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'>1</span></span> " +
" <span style='mso-tab-count: 14'> <span style='mso-field-code:
NUMPAGES '></span> " +
" </p></div><![endif]-->" +
"</div></body></html>";


Now force this content to be downloaded as your word document using response.

//Force this content to be downloaded as a Word document
Response.AddHeader("Content-Type", "application/msword");
//filename specify the name of word document of your choice
Response.AddHeader("Content-disposition", "attachment; filename=myWordDoc.doc");
Response.Charset = "";
Response.Write(strDocBody);


Add the whole content on page load event or button click event as example code.

Example:

protected void Page_Load(object sender, EventArgs e)
{
string strDocBody;

try { strDocBody = "<html " + "xmlns:o='urn:schemas-microsoft-com:office:office' " + "xmlns:w='urn:schemas-microsoft-com:office:word'" + "xmlns='http://www.w3.org/TR/REC-html40'>" + "<head>" + "<title>Dynamic Generated Document</title>";

strDocBody = strDocBody + "<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:Zoom>100</w:Zoom>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->";

strDocBody = strDocBody + "<style> @page" + "{size:8.5in 11.0in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt;" + " margin:0.75in 0.50in 0.75in 0.50in ; " + " mso-header-margin:.5in; " + " mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + " {page:Section1;}" + "p.MsoFooter, li.MsoFooter, div.MsoFooter{margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}" + "p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}" + "-->" + "</style>" + "</head>";

strDocBody = strDocBody + "<body lang=EN-US style='tab-interval:.5in'>" + "<div class=Section1>" + "<h1>This is my Heading</h1>" + "<h2>This is my Sub Heading</h2>" + "<p style='color:navy;'> This is blue text</p>" + "<p style='font-weight:bold; color:green;'><u> This is green bold underlined text </u></p>" + "<p style='color:red'><I>" + DateTime.Now + "</I></p>" + "<img img width=217 height=162 id='myImg' src='C:/WINDOWS/Web/Wallpaper/Autumn.jpg'>" + "<!--[if supportFields]>" + "<div style='mso-element:header' id=h1><p class=MsoHeader><span style='mso-tab-count:4'></span><span style='mso-field-code: PAGE '></span> </p></div>" + "<div style='mso-element:footer' id=f1> " + "<p class=MsoFooter style='border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0in;mso-padding-alt:0in 0in 1.0pt 0in'><o:p> </o:p></p> " + "Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'>1</span></span> of <span style='mso-field-code: NUMPAGES '></span>" + " <span style='mso-tab-count: 12'> <span style='mso-field-code: DATE '></span> " + " </p></div><![endif]-->" + "</div> </body> </html> ";

//Force this content to be downloaded as a Word document

Response.AddHeader("Content-Type", "application/msword");

Response.AddHeader("Content-disposition", "attachment; filename=mydoc.doc");

Response.Charset = ""; Response.Write(strDocBody);


}

catch (Exception ex)

{

Response.Write(ex.Message);

}

}

There is a trick to download your aspx page as word document without coding so much but as the aspx. If you remove the closing body and html tag from the above program and define first three blocks i.e page HTML, page layout definitions and style definitions on the page load event then your aspx page contents will be downloaded as word document. Other contents along with header and footer will defined as the aspx page. You can find it in the next post.

Tuesday, March 13, 2007

Welcome You All!

Dear Friends!

You are welcome to my blog. I am a Software Analyst and have more than three years of work experience in various technologies (like .NET, JAVA, XML, SQL Server etc.) on web applications, distributed applications, business intelligence applications and data mining applications. In my early days of programming, I took help from many blogs and forums. This blog is for those who are new to technology and looking for some initial help. If you are new in web technology you can ask questions to me. All intermediate and expert programmers are welcome to share their views, suggestions and solutions in this blog.

Enjoy Programming!

Arun