Monday, September 21, 2009

Connection Strings

Public Shared Function RetSqLiteCn() As SQLiteConnection
Dim path_to_db As String = AppDomain.CurrentDomain.BaseDirectory & "xxx.sqlite"
Dim sqlite_connection As New SQLiteConnection("Data Source=" & path_to_db & ";Version=3;New=True;Compress=True;")
Return sqlite_connection
End Function
************************************************************************

'ASP.Net

Add this in Web.Config
|connectionStrings|
|add name ="xxxb" connectionString ="Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|xxx" providerName="System.Data.SqlClient"|
|connectionStrings|

SqlConnection sqlCn = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ToString());
return sqlCn;

************************************************************************
'Windows

Public Shared Function RetSqlModeCn(ByVal strDbName As String, ByVal blnIntgSec As Boolean) As SqlConnection
Dim sqlCnB As New SqlConnectionStringBuilder
sqlCnB.DataSource = SystemInformation.ComputerName & "\SQLEXPRESS"
sqlCnB.InitialCatalog = strDbName
sqlCnB.IntegratedSecurity = blnIntgSec
sqlCnB.UserID = "sa"
sqlCnB.Password = "xxxx"
sqlCnB.UserInstance = False
Dim sqlCn As New SqlConnection(sqlCnB.ConnectionString)
Return sqlCn
End Function

An easy reuseable class for Insert/Delete/Update operation using Dictionary


Imports System.Windows.Forms
Imports System.Data.SQLite

Public Class GenericOpt

'DataGridView to show the data
Private dgGeneric As DataGridView
'SqlConnection to connect to the database
Private sqlCnGeneric As SQLiteConnection
'DataAdapter to fetch the data
Private daGeneric As SQLiteDataAdapter
'Dataset to hold the data
Private dsGeneric As DataSet
'Table to perform the operation on
Private strGenericTblNm As String
'Select Query
Private strGenericSelectQuery As String
'Columns to Hide in the datagridview
'Private arrClmnsToHide As DataColumn()
'Columns to Make Read Only
'Private arrClmnsToReadOnly As DataColumn()
'Collection for the Add Data Operation
Private InputData As Dictionary(Of String, String)
'Get the Current Index of Row in the dataset
Private intRowIndex As Integer
'Form Object to which the BindingContext is associated with
Private oForm As Form

Public Shared m_Updtext As String = "Edit"
'GetEdit/Update text
Public Property GetUpdTxt() As String
Get
Return m_Updtext
End Get
Set(ByVal value As String)
m_Updtext = value
End Set
End Property


'Getter and Setter method for InputData
Public Property GetInputData() As Dictionary(Of String, String)
Get
Return InputData
End Get
Set(ByVal value As Dictionary(Of String, String))
InputData = value
End Set
End Property

'Getter and Setter method for Form Object
Public Property GetFormObject() As Form
Get
Return oForm
End Get
Set(ByVal value As Form)
oForm = value
End Set
End Property
Private m_Query As String
Public Property GetQuery() As String
Get
Return m_Query
End Get
Set(ByVal value As String)
m_Query = value
End Set
End Property

Public Sub New()

End Sub

Public Sub New(ByVal strParamTblNm As String, ByVal strParamSelectQuery As String, ByVal dgParamGridView As DataGridView, ByVal oParamForm As Form)

sqlCnGeneric = Utility.RetSqLiteCn()
strGenericTblNm = strParamTblNm
strGenericSelectQuery = strParamSelectQuery
daGeneric = New SQLiteDataAdapter(strGenericSelectQuery, sqlCnGeneric)
Dim cmdB As New SQLiteCommandBuilder(daGeneric)
dsGeneric = New DataSet
dgGeneric = dgParamGridView
oForm = oParamForm
FillDataSet()

End Sub

Public Sub New(ByVal strParamTblNm As String, ByVal strParamSelectQuery As String)
sqlCnGeneric = Utility.RetSqLiteCn()
strGenericTblNm = strParamTblNm
strGenericSelectQuery = strParamSelectQuery
daGeneric = New SQLiteDataAdapter(strGenericSelectQuery, sqlCnGeneric)
Dim cmdB As New SQLiteCommandBuilder(daGeneric)
dsGeneric = New DataSet
FillDataSet()
End Sub

Public Function FillDataSet() As DataSet
Try
sqlCnGeneric.Close()
sqlCnGeneric.Open()
dsGeneric.Clear()
daGeneric.FillSchema(dsGeneric, SchemaType.Source, strGenericTblNm)
daGeneric.Fill(dsGeneric, strGenericTblNm)

Catch ex As Exception
MsgBox(ex.ToString)
Finally
sqlCnGeneric.Close()
End Try
Return dsGeneric
End Function

Public Sub FillDg()
If dgGeneric IsNot Nothing Then
dgGeneric.DataSource = dsGeneric
dgGeneric.DataMember = strGenericTblNm
End If
End Sub

Public Sub AddData()
Try
Dim addRow As DataRow = dsGeneric.Tables(strGenericTblNm).NewRow
If Me.InputData IsNot Nothing Then
For Each tempData As KeyValuePair(Of String, String) In Me.InputData
addRow(tempData.Key) = tempData.Value
Next
dsGeneric.Tables(strGenericTblNm).Rows.Add(addRow)
daGeneric.Update(dsGeneric, strGenericTblNm)
End If

FillDataSet()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Public Sub AddDataByVerify(ByVal strPrimaryKeyData As String)
Try
Dim addRow As DataRow = dsGeneric.Tables(strGenericTblNm).NewRow
If Me.InputData IsNot Nothing Then

If dsGeneric.Tables(strGenericTblNm).Rows.Find(strPrimaryKeyData) Is Nothing Then
For Each tempData As KeyValuePair(Of String, String) In Me.InputData
addRow(tempData.Key) = tempData.Value
Next
dsGeneric.Tables(0).Rows.Add(addRow)
daGeneric.Update(dsGeneric, strGenericTblNm)
End If
End If

FillDataSet()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub



Public Sub DeleteData()

Try
intRowIndex = oForm.BindingContext(dsGeneric, strGenericTblNm).Position
If intRowIndex = -1 Then
MsgBox("Please Select a Valid Record!", MsgBoxStyle.Critical, "Information")
Else
If MsgBox("Are you sure you want to delete this record?", MsgBoxStyle.YesNo, "Confirmation") = MsgBoxResult.Yes Then
oForm.BindingContext(dsGeneric, strGenericTblNm).RemoveAt(intRowIndex)
daGeneric.Update(dsGeneric, strGenericTblNm)
MsgBox("Record Deleted Successfully!")
End If
End If

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Public Function GetRowIndex() As Integer
Return oForm.BindingContext(dsGeneric, strGenericTblNm).Position
End Function

Public Sub UpdateData()
Try
oForm.BindingContext(dsGeneric, strGenericTblNm).EndCurrentEdit()
daGeneric.Update(dsGeneric, strGenericTblNm)
MsgBox("Record Updated Successfully!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Public Sub UpdateData(ByVal objDataToUpdate As Object)
dsGeneric.Tables(0).Rows(0).ItemArray() = objDataToUpdate
daGeneric.Update(dsGeneric, strGenericTblNm)
MsgBox("Record Updated Successfully!")
End Sub
Public Sub SearchData(ByVal strSearchQuery As String, Optional ByVal clmnParamToHide As Int16() = Nothing, Optional ByVal clmnParamToReadOnly As Int16() = Nothing)

Try
sqlCnGeneric.Close()
sqlCnGeneric.Open()
'dgGeneric.DataSource = Nothing
daGeneric.SelectCommand.CommandText = strSearchQuery
dsGeneric.Clear()
Dim cmdB As New SQLiteCommandBuilder(daGeneric)
daGeneric.Fill(dsGeneric, strGenericTblNm)
'dgGeneric.DataSource = Nothing
If dsGeneric IsNot Nothing Then
dgGeneric.DataSource = dsGeneric
dgGeneric.DataMember = strGenericTblNm

If clmnParamToHide IsNot Nothing Then
For Each i As Int16 In clmnParamToHide
dgGeneric.Columns(i).Visible = False
Next
End If

If clmnParamToReadOnly IsNot Nothing Then
For Each i As Int16 In clmnParamToReadOnly
dgGeneric.Columns(i).ReadOnly = True
Next
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
Finally
sqlCnGeneric.Close()
End Try
End Sub

Public Sub MoveRowUp()
If oForm IsNot Nothing Then
oForm.BindingContext(dsGeneric, strGenericTblNm).Position -= 1
Else
MsgBox("Form object not initialized!")
End If
End Sub

Public Sub MoveRowDown()
If oForm IsNot Nothing Then
oForm.BindingContext(dsGeneric, strGenericTblNm).Position += 1
Else
MsgBox("Form object not initialized!")
End If

End Sub
Public Sub MoveRowFirst()
If oForm IsNot Nothing Then
oForm.BindingContext(dsGeneric, strGenericTblNm).Position = 0
Else
MsgBox("Form object not initialized!")
End If

End Sub

Public Sub MoveRowLast()
If oForm IsNot Nothing Then
oForm.BindingContext(dsGeneric, strGenericTblNm).Position = oForm.BindingContext(dsGeneric, strGenericTblNm).Count - 1
Else
MsgBox("Form object not initialized!")
End If
End Sub

End Class

Reusable Xml Operation Class

Imports System.Xml

Public Class Oxml

Private m_XmlDoc As XmlDocument
Private m_xmlWriter As XmlTextWriter
Private m_XmlPath As String
Private m_hshWE As Hashtable
Private m_KeyWE As String

Private m_strDE() As String
Private m_hshUE As Hashtable

Private m_StartElement As String
Private m_SelectionNode As String

'Constructor
Public Sub New()

End Sub

'Constructor
Public Sub New(ByVal xmlpath As String, ByVal startelement As String, ByVal hshWE As Hashtable, _
ByVal keyWE As String, ByVal strDE() As String, ByVal hshUE As Hashtable, ByVal selectionnodes As String)
m_XmlDoc = New XmlDocument()
m_XmlPath = xmlpath

m_hshWE = hshWE
m_KeyWE = keyWE

m_strDE = strDE
m_hshUE = hshUE

m_SelectionNode = selectionnodes
End Sub

'Constructor
Public Sub New(ByVal xmlpath As String, ByVal startelement As String, ByVal hshWE As Hashtable, _
ByVal keyWE As String, ByVal selectionnode As String)
m_XmlDoc = New XmlDocument()
m_XmlPath = xmlpath
m_StartElement = startelement
m_hshWE = hshWE
m_KeyWE = keyWE
m_SelectionNode = selectionnode

End Sub

'Check the path of the XML file
Private Function IsXMLPathValid() As Boolean

If IO.File.Exists(m_XmlPath) = True Then
Return True
Else
Return False
End If

End Function

'Write Element

Public Sub WriteElement()

If m_KeyWE = String.Empty Then
MsgBox("Key Write Element Invalid!")
Return
End If

If m_hshWE Is Nothing Then
MsgBox("Write Elements Invalid!")
Return
End If


Dim strMainIdentifier As String = m_hshWE.Item(m_KeyWE).ToString

If IsXMLValid() = True AndAlso HasElement(strMainIdentifier) = False Then
If IsXMLPathValid() = False Then
MsgBox("XML File Invalid!")
Else
m_XmlDoc.Load(m_XmlPath)
Dim root As XmlNode = m_XmlDoc.DocumentElement
Dim xmlG As XmlElement = m_XmlDoc.CreateElement("Group")
'xmlG.InnerText = String.Empty
For Each de As DictionaryEntry In m_hshWE
Dim xmlE As XmlElement = m_XmlDoc.CreateElement(de.Key)
xmlE.InnerText = de.Value
xmlG.AppendChild(xmlE)
'root.AppendChild(xmlE)
root.AppendChild(xmlG)
m_XmlDoc.Save(m_XmlPath)
Next
'root.AppendChild(m_XmlDoc.CreateNode(XmlNodeType.EndElement, "/Group", Nothing))
End If

End If

If IsXMLValid() = False AndAlso HasElement(strMainIdentifier) = False Then
If m_StartElement = String.Empty Then
MsgBox("Start Element Invalid!")

Else
m_xmlWriter = New XmlTextWriter(m_XmlPath, Nothing) 'Application.StartupPath & "\InternalXML\URLCat.XML", Nothing)
With m_xmlWriter
.Indentation = 4
.IndentChar = " "c
.Formatting = Formatting.Indented
.WriteStartDocument()
.WriteStartElement(m_StartElement)
.WriteStartElement("Group")
For Each de As DictionaryEntry In m_hshWE
.WriteElementString(de.Key, de.Value)
Next
.WriteEndElement()
.WriteEndElement()
.WriteEndDocument()
.Flush()
.Close()
End With
End If
End If
End Sub

'Delete the element
Public Sub DeleteElement()
If IsXMLPathValid() = False Then
MsgBox("XML File Invalid!")
Else
m_XmlDoc.Load(m_XmlPath)
For Each s As String In Me.m_strDE
For Each xmlnd As XmlNode In m_XmlDoc.SelectSingleNode(m_SelectionNode)
If String.Compare(xmlnd.InnerText, s, True) = 0 Then
'xmlnd.SelectSingleNode("/URLCategories/Category").ParentNode.RemoveAll()
xmlnd.ParentNode.RemoveChild(xmlnd)
End If
Next
m_XmlDoc.Save(m_XmlPath)
Next
End If

End Sub

Public Sub UpdateElement(ByVal elementname As String)

End Sub

'Check to see if the XML document is valid
Public Function IsXMLValid() As Boolean

Dim xmlDoc As New XmlDocument()
Try
xmlDoc.Load(m_XmlPath)
Catch ex As Exception
MsgBox(ex.Tostring)
Return False
End Try
Return True

End Function

'Check to see if the XML has that element
Public Function HasElement(ByVal elementname As String) As Boolean

Dim blnHasElement As Boolean
Try
If IO.File.Exists(m_XmlPath) Then
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(m_XmlPath)

For Each xmlnd As XmlNode In xmlDoc.SelectNodes(m_SelectionNode)
If String.Compare(xmlnd.InnerText, elementname, True) = 0 Then
blnHasElement = True
Exit For
End If
Next
End If
Catch ex As Exception
Return False
End Try
Return blnHasElement

End Function

End Class

Thursday, September 3, 2009

XBM (Written on C# & based upon MVC architecture)


:Download it here(Tested with WinXP only)
:Extract the zipped folder using any zip handler and run XBM-Beta.exe from inside the zip folder.

:Requisites(For WinXP): Install the .Net framework below first

Please report any bug/suggestions to: xbiplav@hotmail.com

This software is still in beta version and many enhancements/functionalities are yet to come.

Why this software?

- Are your tired of losing track of your favorite urls/bookmarks or websites?
- Are you tired of scrolling through a long list of bookmarks in your browser while searching for your favorite boomark?
-Are you looking for a software to locate/manage your favorite web addresses by category or an identifer ?

Then, this software may be what you are looking for.

Features:

What you can do?

1. Give your favorite URL a suitable name-an identifier.
2. Manage URL by category.
3.Search URL by keyword or identifier.
4.Locate your favorite URLS by category.
5.Open the URL in the browser while skimming through a number of URLs.
6.Locate URLS by import sources.
7.Supports upto 2 Terabyte of URL/Web Addresses storage.

Built in functionalities

1. Import boomarks from Mozilla Firefox(3.0 and higher) & Internet Explorer.
2.Imports links from a folder such as a favorite folder
3.Import links from a textfile.
4.Import urls from a XML file.
5.Manually add Urls.
6.Export links to the XML file.