Recent Posts

Sunday, August 24, 2008

How to insert record in SQL server database from VB.Net?

First go to following link
Secure connection and easy transaction handling in .Net

to understand the logic for the connection
and to get the idea of what objDB means.

Just create a objDB class from the above link provided and use the following code
to insert a record in the table named dog.

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSingle.Click
Dim str As String
Dim objDB As clsObjDB

Try
objDB = New clsObjDB
str = "insert into Dog (DogName,Active)values('Lika',1) "


objDB.ExecuteQuery(str)
objDB = Nothing

messagebox.Show("Successfully Inserted Single row in dog!!" & vbCrLf)

Catch ex As Exception
Messagebox.show(ex.ToString())

objDB = Nothing
End Try

End Sub

In the above function I have just created a new instance of clsObjDB andpassed the sql query in the ExecuteQuery function and that's it, it does the rest of the work.


Read more!

Thursday, August 21, 2008

How to handle and log Asp.Net Error in Event Viewer ?

You can just use the following to descently handle you ASP.net error messages and log the error in the event viewer .

Also if you want to add the email function in the function.

the following code is the simplest way to handle error messages in ASP.net:


using System;
using System.Diagnostics;


void Page_Error(Object sender, EventArgs args)
{
Response.Write("Error:\n");
Exception e = Server.GetLastError();

EventLog.WriteEntry("Test Web",
"MESSAGE: " + e.Message +
"\nSOURCE: " + e.Source +
"\nFORM: " + Request.Form.ToString() +
"\nQUERYSTRING: " + Request.QueryString.ToString() +
"\nTARGETSITE: " + e.TargetSite +
"\nSTACKTRACE: " + e.StackTrace,
EventLogEntryType.Error);

Trace.Write("Message", e.Message);
Trace.Write("Source", e.Source);
Trace.Write("Stack Trace", e.StackTrace);
Response.Write("Sorry, an error was encountered.");
Context.ClearError();
}


Read more!

Wednesday, August 20, 2008

VB Split() function in C# (Stupid mistake made while converting! Solved now)

I was trying to convert the vb Split() function to C#
and while going through google it seemed pretty much easy but my function is not working or giving me errors.

I went through it more than 30 mins and later found out my mistake that I will
explain in the end of this post.

Ok here is the vb code that uses the split function to split a string line.

Dim strLine As String
strLine = "DataSource = GetDatabase"
lblMsg.Text = getTextLine(strLine)


Public Function getTextLine(ByVal strline As String) As String

Dim spChar As String
spChar = "="
Dim values() As String = Split(line, spChar)
Dim reply As String = values(1).Trim

Return reply
End Function

and the exact replica of this code in C# is :

string strLine;
strLine = "DataSource = GetDatabase";
lblMsg.Text = getTextLine(strLine);

public string getTextLine(string line)
{
char spChar = '=';


string[] arr = line.Split(spChar);
string reply = arr[1];
return reply;


}

Now where i spent my 30 mins making following mistakes:

1. The big mistake what i didn't figure out for long was i did

char spChar= "=";

2. And so I ended up declaring spChar as string. Then the problem worsened.


Read more!

Blog for Programmers (Prateek Regmi): How to get data from the Database using dataset in .Net

Blog for Programmers (Prateek Regmi): How to get data from the Database using dataset in .Net


Read more!

Monday, August 18, 2008

How to get data from the Database using dataset in .Net

First go to following link
Secure connection and easy transaction handling in .Net

to understand the logic for the connection
and to get the idea of what objDB.getDataSet means.

Then use the following function to get the values from the table named Books:


Private Function GetBooks() As Boolean
Dim strSql As String
Dim ds As DataSet

dim objBook as clsBook()

Try
strSql = "SELECT * from Books"

ds = objDB.getDataset(strSql)
If ds.Tables(0).Rows.Count > 0 Then
For Each row As DataRow In ds.Tables(0).Rows
objBook = new clsBook()

objBook.BookID = row("BookingID")
ObjBook.BookName = row ("BookName")
'etc

Next
GetBooks=true
Else

GetBooks = False
End If
Catch ex As Exception

Throw ex
End Try
End Function


Read more!