Sunday, January 17, 2010

SFTP Client OpenSource and DLL

You can find opensource code to for SecureFTP Server in the below said link
where the dlls can be used in .Net to access files in SecureFTP Server

http://www.tamirgal.com/blog/page/SharpSSH.aspx

Thanks to Tamir Gal

Note: In the wrapper class the rm function has to be called for remove file function in the source code and compile

Tuesday, January 12, 2010

Error Log in Text File in Asp.Net 2.0

Simple vb.net code function used to log error in text file which rises in our application

Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Globalization

Public Class ErrorHandler

Public Shared Sub WriteError(ByVal errorMessage As String)
Try
Dim path As String = "~/Error/" & DateTime.Today.ToString("dd-mm-yy") & ".txt"
If (Not File.Exists(System.Web.HttpContext.Current.Server.MapPath(path))) Then
File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close()
End If
Using w As StreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path))
w.WriteLine(Constants.vbCrLf & "Log Entry : ")
w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture))
Dim err As String = "Error in: " & System.Web.HttpContext.Current.Request.Url.ToString() & ". Error Message:" & errorMessage
w.WriteLine(err)
w.WriteLine("__________________________")
w.Flush()
w.Close()
End Using
Catch ex As Exception

End Try

End Sub
End Class



This Code should be written in App_Code Folder of Web Application

This function can be called in all Exception Catches
Try
'Your Code Goes Here’
Catch ex as Exception
ErrorHandler.WriteError(ex.Innerexception)
End Try



To Err is Human

Monday, December 21, 2009

Disable button by checking validators and invoke server side event

Scenario: - Check whether any validator is triggered. If triggered then don't postback and don't disable the button. If all validators are valid then disable the button and call the server side event.

Solution:-
To solve this scenario , I found out one simple solution using javascript


<script type="text/javascript" language="javascript">
function fnValidate(obj)
{
for(var i=0; i<Page_Validators.length; i++)
{
ValidatorEnable(Page_Validators[i]);

if (Page_Validators[i].isvalid)
{
obj.disabled=true;
}
else
{
obj.disabled=false;
break;
}
}

if (obj.disabled==true)
{
<%=Page.GetPostBackEventReference(btnSubmit)%> }

}

</script>


Call this javascript function in button's OnClientClick property as follows.


<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="fnValidate(this);" CausesValidation="true" OnClick="btnSubmit_Click"/>

Tuesday, December 1, 2009

Enum to Datatable Function Using Vb.net

In this post I have given simple vb.net snippet to convert Enumeration data to datatable, so that we can use the enumeration datas as master table or lookup table


Shared Function EnumToDataTable(ByVal typEnum As Type) As DataTable
Dim ddlTypnames() As String = [Enum].GetNames(typEnum)
Dim arrddlTypVals As Array = [Enum].GetValues(typEnum)

Dim dt As New DataTable
Dim dr As DataRow

dt.Columns.Add(New DataColumn("values", GetType(Int32)))
dt.Columns.Add(New DataColumn("names", GetType(String)))
Dim i2 As Integer
For i2 = 0 To ddlTypnames.Length() - 1
dr = dt.NewRow()

dr(0) = CInt(arrddlTypVals(i2))
dr(1) = ddlTypnames(i2)

dt.Rows.Add(dr)
Next i2

Return dt
End Function

Using this function

Dim dt As DataTable
dt = EnumToDataTable(GetType(EnumSample))


Happy Coding!

Sunday, November 22, 2009

To Disable Button on Submit Using JavaScript

Simple Javascript Snippet to disable button on submitting to server,
this script has to be placed in the page where the button has to be disabled.
Paste the script below the script manager
This function used to disable the button when the server side button is
clicked and after all other validations.

<script language="javascript" type="text/javascript">
//Set function to execute when the event occurs

..Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
//The function which will disable the button

function BeginRequestHandler(sender, args)
{
document.getElementById("$lt%=btnSave.ClientId %>").disabled=true;
//Insert here the script that needs to be executed
//before the request is sended to the server
}

</script>


Add This in WebConfig File

<httphandlers>

<add validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" path="ScriptResource.axd" verb="GET,HEAD">
</httphandlers>
<httpmodules>
<add type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="ScriptModule">
</httpmodules>

Tuesday, November 3, 2009

To Read Line By Line of Text File in VB.net 2.0

In this Post I have shown the simple code snippet for reading a text file line by line.

Dim strFileName As String = String.Empty
Dim fs As FileStream = Nothing
Dim m_streamReader As StreamReader = Nothing
Dim strCompany As String = String.Empty

fs = New FileStream(strFilePath, FileMode.Open, FileAccess.Read)
m_streamReader = New StreamReader(fs)
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin)

Do
strContent = m_streamReader.ReadLine()

Loop While (Not m_streamReader.EndOfStream)

Thursday, October 8, 2009

Simple Delay Function Using VB.Net

Simple Delay Function Using VB.Net

Private Sub Delay(ByVal ms As Integer)
Dim time As Integer = Environment.TickCount
Do While (True)
If Environment.TickCount - time >= ms Then
Exit Do
End If
Loop
End Sub

Happy Coding!