Wednesday, May 4, 2011

How to debug ASP page in VS 2010


How to debug ASP pages in VS 2010.

This is one way to debug the application but there could be another way also

· Step 1

· Open the site in IE

· Click Tools à Internet Options

· Click on the Advanced Tab

· Make sure that the below two options are Unchecked

o Disable Script debugging (Internet Explorer)

o Disable script debugging (Other)

· Step 2

· Open IIS à Right click on the Site/Virtual Directory à Click Properties

· Click on the “Configuration” button in the Virtual Directory Tab in the Application Settings section

· Click on the Debugging tab and make sure that the two check boxes shown are checked under the Debugging Flags section


· Step 3

o Run the site in IE

o Open the ASP page that you want to debug

oClick the Tools à Attach Processes

o In the Attach Processes Dialog boxes, check the “Show processes from all users” checkbox

o Click the dllhost.exe and then click on the “Select…” button in the Attach to section and make sure that “Script To” option is checked

o Click ok and click ok again for the warning message that shows the Attach message

o Now, put the break point in your ASP page where the application wants to break

Friday, April 22, 2011

Search and replace text in multiple files using powershell

I was trying to find and replace a text in multiple files using the powershell script. I got couple of references online and arrived at the below script. I agree that this can be further improvised but could be used as a starting point or quick and dirty solution


$match = "text1 test"
$replacement = ""
$match1 = "text2
test"
$replacement1 = ""
$match2 = "text3 test"
$replacement2 = ""
$match3 = "text4 test"
$replacement3 = ""
$files = Get-childItem
–filter *.txt -path "C:\foo" -recurse


foreach($file in $files)
{
((Get-Content $file.fullname) -creplace
$match,$replacement)
set-content $file.fullname

((Get-Content $file.fullname) -creplace
$match1,$replacement1)
set-content $file.fullname
((Get-Content
$file.fullname)
-creplace $match2,$replacement2) set-content
$file.fullname
((Get-Content $file.fullname) -creplace
$match3,$replacement3) set-content $file.fullname
}


References

powershellcommunity

Friday, March 5, 2010

Reverse the words in a given string

Program that reverses a given sentence or word.
For Ex: Calling the function with the name "My Name is Prashanth" will return you "yM emaN si htnahsarP"

public string ReverseSentence(string OrigSentence)
{
StringBuilder reversedString = new StringBuilder();
char[] chars = OrigSentence.ToCharArray();
string word = string.Empty;

int wordstart , wordend , start , end ;

wordstart = wordend = start = end = 0;

end = OrigSentence.Length;

if (end > 0)
{
wordstart = start;
while (start < end)
{
if (chars[start] != ' ')
{
word += chars[start];
start++;
}
else
{
//wordend = start - 1;
reversedString.Append(ReverseWord(word));
word = string.Empty;

while (start < end && chars[start] == ' ')
{
reversedString.Append(chars[start]);
start++;
}
}
}

if (word.Length > 0)
{
reversedString.Append(ReverseWord(word));
}
}

return reversedString.ToString();
}

private string ReverseWord(string actualWord)
{
int start = 0;
int end = actualWord.Length-1;
char[] chars = actualWord.ToCharArray();
char tempchar;

while (start < end)
{
tempchar = chars[start];
chars[start] = chars[end];
chars[end] = tempchar;
start++;
end--;
}

string reveresedWord = new string(chars);

return reveresedWord;
}

Thursday, October 1, 2009

Validating XML against an XSD, .net

Following code will validate an XML file agaisnt an XSD stored in a file system and stored all the errors encoutered


Public Function ValidatingProcess(ByVal XSDPath As String, ByVal XMLPath As String, ByVal ValidationErrorsSavePath As String)

Try

' 1- Read XML file content
Reader = New XmlTextReader(XMLPath)

' 2- Read Schema file content
SR = New StreamReader(XSDPath)

' 3- Create a new instance of XmlSchema object
Dim Schema As XmlSchema = New XmlSchema



' 4- Set XmlSchema object by calling XmlSchema.Read() method
' Schema = XmlSchema.Read(SR, new ValidationEventHandler(ValidatingReader_ValidationEventHandler));
Schema = XmlSchema.Read(SR, New ValidationEventHandler(AddressOf ValidatingReader_ValidationEventHandler))

'Addhandler SR,

'AddHandler Reader.ValidationEventHandler, AddressOf Me.ValidatingReader_ValidationEventHandler

' 5- Create a new instance of XmlValidationReader object
Dim ValidatingReader As XmlValidatingReader = New XmlValidatingReader(Reader)

' 6- Set ValidationType for XmlValidationReader object
ValidatingReader.ValidationType = ValidationType.Schema

' 7- Add Schema to XmlValidationReader Schemas collection
ValidatingReader.Schemas.Add(Schema)

' 8- Add your ValidationEventHandler address to
' XmlValidationReader's ValidationEventHandler
'ValidatingReader.ValidationEventHandler += New ValidationEventHandler(ValidatingReader_ValidationEventHandler)
AddHandler ValidatingReader.ValidationEventHandler, AddressOf ValidatingReader_ValidationEventHandler

test:

Try

' 9- Read XML content in a loop
While (ValidatingReader.Read())

End While
Catch

GoTo test
End Try

SaveErrorsToAFile(ValidationErrorsSavePath, Me.Results)


'Handle exceptions if you want
Catch AccessEx As UnauthorizedAccessException

Throw AccessEx

Catch Ex As Exception
Throw Ex
End Try

End Function


Private Sub ValidatingReader_ValidationEventHandler(ByVal sender As Object, ByVal args As System.Xml.Schema.ValidationEventArgs)

' 10- Implement your logic for each validation iteration
Dim strTemp As String
strTemp = "Line: " + Me.Reader.LineNumber.ToString() + " - Position: " + Me.Reader.LinePosition.ToString() + " - " + args.Message

Me.Results.Add(strTemp)
End Sub


Private Function SaveErrorsToAFile(ByVal filepath As String, ByVal errors As ArrayList) As Boolean

Dim blnSuccess As Boolean = False

Try

Dim tw As TextWriter = New StreamWriter(filepath, False)

'/ write a line of text to the file
Dim e As IEnumerator = errors.GetEnumerator()

While (e.MoveNext())
tw.WriteLine(e.Current.ToString())
End While
' close the stream
tw.Close()
blnSuccess = True

Catch
blnSuccess = False
End Try

Return blnSuccess
End Function



References

http://aspalliance.com/941

Friday, May 22, 2009

Exporting datatable to an Excel Sheet

Simple function to export the data in a Datatable to an excel sheet.

#region Custom References
using System.Xml;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
#endregion


public override bool GenerateOutput(System.Data.DataTable sourceData, string outputfilepath)
{
bool blnSuccess = false;
try
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlApp.DisplayAlerts = false;
xlApp.Visible = false;

// delete the sheet if already exists
try
{
xlWorkSheet = (Worksheet)xlWorkBook.Sheets["test"];
if (xlWorkSheet != null)
{
xlWorkSheet.Delete();
}
}
catch
{

}

xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlWorkSheet.Name = "test";

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item("test");

for (int rowCount = 1; rowCount <= sourceData.Rows.Count; rowCount++)
{
for (int readerColIndex = 1; readerColIndex <= sourceData.Columns.Count; readerColIndex++)
{
xlWorkSheet.get_Range(xlWorkSheet.Cells[rowCount, readerColIndex], xlWorkSheet.Cells[rowCount, readerColIndex]).Value2 = sourceData.Rows[rowCount - 1].ItemArray[readerColIndex - 1].ToString();
}
}

xlWorkBook.SaveAs(outputfilepath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

xlWorkBook.Close(true, misValue, misValue);
xlApp.DisplayAlerts = true;
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

blnSuccess = true;
}
catch (Exception ex)
{
blnSuccess = false;
throw new Exception(ex.ToString());
}

return blnSuccess;
}

public static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
//MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}

Monday, May 18, 2009

How to convert a SqlDatareader to a Dataset

How to Copy a datareader to a dataset

private DataSet CopyReaderToDataSet(SqlDataReader reader)
{
DataSet dataSet = new DataSet();
System.Data.DataTable schemaTable = reader.GetSchemaTable();
System.Data.DataTable dataTable = new System.Data.DataTable();

for (int cntr = 0; cntr < schemaTable.Rows.Count; ++cntr)
{
DataRow dataRow = schemaTable.Rows[cntr];
string columnName = dataRow["ColumnName"].ToString();
Type colType = dataRow["ColumnName"].GetType();
DataColumn column = new DataColumn(columnName, colType);
dataTable.Columns.Add(column);
}

dataSet.Tables.Add(dataTable);

while (reader.Read())
{
DataRow dataRow = dataTable.NewRow();
for (int cntr = 0; cntr < reader.FieldCount; ++cntr)
{
dataRow[cntr] = reader.GetValue(cntr);
}
dataSet.Tables[dataTable.TableName.ToString()].Rows.Add(dataRow);
}

return dataSet;
}

Reference: Roni's blog, added couple of lines missing in his blog

Friday, May 15, 2009

How to update the custom Task property at runtime?

I am currently working on to find a way to update the properties of the custom task view at runtime. I have a custom task with a read-only property in the view. But that property will be chaning during the runtime by the Execute method of the task. I am unable to display the changed value during the runtime by the view. I need to update this value in the view to get it stored in the checkpointfile of the package so that the task can support the checkpointfile.