Friday, June 27, 2008

ASP.NET Deployment true

ASP.NET 2.0 comes with a new setting (< deployment retail = "true" />) in machine.config which disables debugging, tracing and error messages.

Tracing as well as asp.net error messages should not visible to the end user due to security reasons, so its always recommended to turn custom errors and tracing off in web.config.

Sometimes it is not possible to check all the web.config in an application individually and verify whether compilaion debug is true or false (< compilation debug=”true”/ >) in web.config, moreover its tedious process. To get rid of this pain, ASP.NET 2.0 has <deployment> section in machine.config file.

Turn this switch on

< system.web >
< deployment retail=”true”/ >
< /system.web >

it will disable the < compilation debug=”true”/> of web.config file.

In fact < deployment retail=”true”/ > overrides < compilation debug=”true”/ >.

Refer : http://msdn.microsoft.com/en-us/library/system.web.configuration.deploymentsection.retail(VS.80).aspx for more information

Thursday, June 26, 2008

Better way to maintain Rich Text Format in SQL

In my previous blog i discussed how can we maintain rich text format in SQL.

I got better and easier way to maintain rich text format in SQL.

Here is the code to save the rich text format in SQL (Create a table myRTF with column name Descriptionrtf):

string conString = "DB Connection string";
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter da = new SqlDataAdapter("Select Descriptionrtf From myRTF ", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

char[] arr = richTextBox1.Rtf.ToCharArray();
string str = string.Empty;
for(int i = 0; i < arr.Length; i++)
str += arr[i].ToString();

da.Fill(ds,"MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].NewRow();
myRow["Descriptionrtf"] = str;
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");
con.Close();

Code to load date from SQL to rich text box :

string conString = "DB Connection string";
SqlConnection connection = new SqlConnection(conString );
SqlCommand command = new SqlCommand("Select Descriptionrtf From MyImages", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
try
{
richTextBox1.Rtf = reader.GetString(0) ;
}
catch(Exception ex1)
{
MessageBox.Show(ex1.Message);
}
}
reader.close();

Happy Coding :)

Tuesday, June 24, 2008

IIS : How Anonymous access works?

Let’s try to understand how anonymous access works in IIS

1. Create a simple asp page and put <%=DATE()%> in it save it as test.asp in the following location C:\Inetpub\wwwroot\TestAnonymous (Create TestAnonymous directory in C:\Inetpub\wwwroot).
2. Create a virtual directory with TestAnonymous and map it to
C:\Inetpub\wwwroot\TestAnonymous.
3. Create a user testuser using computer management.

4. Now open IIS and go to the directory security tab and click edit button in Anonymous Access frame and change user name to testuser(which we created using computer management) and give the same password that has been provided during creation of testuser, screen should look like below.

5. Make sure no other option is selected(like: Allow IIS to control password, Digest, Basic, Integrated).
6. Try to browse test.asp page, page should come on the browser.
7. Now change the password of testuser in IIS not in computer management i.e. provide wrong password in IIS.
8. Again make sure no other option is selected(like: Allow IIS to control password, Digest, Basic, Integrated).
9. Try to browse test.asp page, page should not come on the browser.
10. Now check Allow IIS to control password,Directory security tab should be like below image.



Above steps make us beleive that anonymous access user should be in sync with the user of the system.

Refer : http://support.microsoft.com/kb/216828 for more information to understand more about Allow IIS to control password.

Monday, June 16, 2008

Maintaining Rich Text Format in SQL

Recently i had to make User Interface with few rich text box controls. Rich text box control allows the user to input formatted text. But this formatted text is of no use if we don't persist the input format. I took few hours to get the solution. I thought why not to write a blog on it.

Create a Form and put the control like below screen shot





Formatting rich text box data :


private void Bold_Click(object sender, System.EventArgs e)
{
Font ft = new Font(richTextBox1.Font,FontStyle.Bold);
richTextBox1.SelectionFont = ft;
}

private void color_Click(object sender, System.EventArgs e)
{
richTextBox1.SelectionColor = Color.Red;
}



Saving rich text box data in SQL :

Create a table(MyImages)with one column(imgField) in SQL with DataType as image.

Create connection to SQL server to save rtf text in database. I preffered to temporarily save rtf in disk with SaveFile of rich text box control, then i created instance of FileStream object and put the data in byte array and saved the byte array in the database table(MyImages), then i deleted the temp(test.rtf) file.

private void savetoDB_Click(object sender, System.EventArgs e)
{

string connectionString = "";
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
string filePath = @"..\..\RTFDoc\test" + DateTime.Now.Millisecond + ".rtf";
richTextBox1.SaveFile(filePath);
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
da.Fill(ds,"MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].NewRow();
myRow["imgField"] = MyData;
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");
con.Close();
File.Delete(filePath);
}


Reading data from SQL :

Create connection to SQL server to get formatted text from database. Then i read the data from database using SQLDataReader, i then created instance of ASCIIEncoding object to convert bytre array in a string object and using string object to fill rich text box control. Then i am closing the reader and connection instance.

private void loadfromDB_Click(object sender, System.EventArgs e)
{
string ConnectionString = "";
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand command = new SqlCommand("Select * From MyImages", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Byte[] rtf = new Byte[Convert.ToInt32((reader.GetBytes(0, 0, null, 0, Int32.MaxValue)))];
long bytesReceived = reader.GetBytes(0, 0, rtf, 0, rtf.Length);
ASCIIEncoding encoding = new ASCIIEncoding();
richTextBox1.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));
}
reader.Close();
connection.Close();
}

Happy Coding :)

Sunday, June 15, 2008

Updating NTEXT,TEXT

The SQL Server's ntext, text, and image data types can hold huge amount of data (up to 2 GB) in a single value.

SQL stores information of TEXT,NTEXT as a pointer in a row of the table.The pointer(textptr) points to the actual data storage page.

SQL server allows to store TEXT,NTEXT in a datarow itself rather than storing pointer in the row. To achieve this we need to enable text in row option using sp_tableoption

Below is the command :

EXEC sp_tableoption 'tablename', 'text in row', 'ON' //Enables the 'text in row' option for table 'tablename'

EXEC sp_tableoption 'tablename', 'text in row', '2000' //Enables the 'text in row' option for table 'tablename', and set the limit to 2000

If the size of NTEXT,TEXT OR Image is greater than specified text in a row ,textptr will be stored in the row.

Example of updating NTEXT :

As NTEXT stores information as a pointer in a row we need a txtptr to update NTEXT data

I have created one table (ntextTbl) with Remarks and Id column

DECLARE @ptrval VARBINARY(16),@Result VARCHAR(8000)

SET @Result = 'Testing NTEXT'

SELECT @ptrval = TEXTPTR(Remarks)FROM ntextTbl WHERE id = 1

UPDATETEXT ntextTbl.Remarks @ptrval NULL NULL @result //This will append 'Testing NText' in existing Remarks row

WRITETEXT ntexttbl.Remarks @ptrval 'Replace complete NTEXT' //This will replace existing remarks row with 'Replace Complete NTEXT'


Happy Coding :)

Sunday, June 8, 2008

pdf in modal dialog box

Few days back i was looking at tech forum in my organisation and i saw one query "how to open pdf in modal dialog box?". Initially i thought this is silly question but no question is silly :).
I tried to open pdf in modal dialog box but i couldn't succeed. I could open html or aspx page in modal dialog box but not pdf. This made me to reach conclusion that pdf doesn't like modal dialog box.
There is a work around of the above problem using which we can open pdf in modal dialog box.
Create a html page(try.html) and embed pdf in iframe like the below code.

< iframe id="iframetest" src="http://localhost:1446/openPdf/csharp_ebook.pdf" height="100%" width="100%"> Test Iframe </iframe>

open the html page using javascript
function popWin(){
window.showModalDialog("http://localhost/openPdf/try.html",'','dialogHeight:650px;dialogWidth:1000px')
}
This will open the pdf in modal dialog box.

Happy Coding :)

Site Meter