Posts

Showing posts from 2013

SaveSetting and GetSetting in C#

How to use SaveSetting() and GetSetting() methods of VB in C#? Lets see, SaveSetting() method is used to store UI input data and GetSetting() method is used to retrieve that stored data. //Save textbox1's value SaveSetting (App.EXEName, "textboxes", "textbox1", textbox1.Text) //Get textbox1's value textbox1.text = GetSetting (App.EXEName, "textboxes", "textbox1", "") SaveSetting stores the data in the Registry (in HKEY_CURRENT_USER|Software|VB and VBA Program Settings|YourAppName). The four parts of the function are the name under which it is stored (App.EXEName in this case), "textboxes" in this example is like the section name in an ini file, "textbox1" is like the key in a line of data in the ini file, and textbox1.text is the value. GetSetting returns the value. The 4th parameter ("" in this case) is optional and it is the default if no registry entry is found. I f you use thes...

C# Optional Parameter

How to use Optional Parameters in Method? An  optional parameter  has a default value. A method with an optional parameter can be called with only some of its parameters specified. Following two code snippet shows how to work with optional parameters in C#. (1) public void method(int a, string optParam="defaultValue") {       //Appropriate code here; } In above example second string parameter  optParam is passed with default value " defaultValue ". When method() is called with only one parameter then second parameter automatically assigned to its default value.       method(25);  // here second parameter gets value " defaultValue ".           or    method(25,"Example"); //here second parameter gets value "Example". Other method to pass optional parameter is, (2) using System.Runtime.InteropServices; public void method(int a, [Optional] string...

Existence of Table in Database

How to Check Existence of Table in Database In SQL Server we can check whether table exist in database or not by using following SQL code snippet.  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'YOUR_TABLE_NAME') AND type in (N'U')) BEGIN   --Your Code Here If table is present.                            END ELSE BEGIN    --Your Code Here If table not exist. END

UPDATE using SELECT Query

UPDATE Query using SELECT Query You can also update table from other table or other SELECT query as shown below. In following example Table2 can be other SELECT query. UPDATE Table1 SET         Column1=Table2.Column1         Column2=Table2.Column2         Column3=Table2.Column3 FROM Table2 WHERE    Table1.ID=Table2.ID Another Example UPDATE Table1 tbl1 SET         tbl1.Column1=tbl2.Column1         tbl1.Column2=tbl2.Column2          FROM (SELECT Column1,Column2 FROM Table2) tbl2 WHERE    tbl1.ID=tbl2.ID
Code for Ping Request from your VB Application Code block written below is used  check whether PC is currently in Network or not using Network.IsAvailable. If PC is in network then it will make Ping Request to specific PC. In below example mPingURL is a variable which is holding IP Address or Name of PC within Network. I f My.Computer.Network.IsAvailable Then    If My.Computer.Network.Ping(mPingURL, 20) Then           foo = True    Else           foo = False           Threading.Thread.Sleep(500)            MsgBox("Constant Internet access is required, currently the Internet not available to access " & mPingURL)                             End If Else    foo = False    MsgBox("Constant Internet access is required, currently ...
Connection Strings For VB.NET Connection string for SQL Server Standard format Server = <YourServerName> ; Initial Catalog = <YourDatabseName> ;  UserId = <YourUserName> ;  Password = <YourPassword> ; Connection to a SQL Server instance Server = <YourServerName/YourInstanceName> ; Initial Catalog = <YourDatabseName> ;  UserId = <YourUserName> ;  Password = <YourPassword> ; Connect using an IP address Data Source = 194.172.200.100,1433 ; Network Library = DBMSSOCN ; Initial Catalog =<Your DataBase> ; User ID =<Your UserName> ; Password = <YourPassword> ; Connection string for MySQL Standard format Server =<Your ServerAddress> ;  Database =<Your DataBase> ;  Uid =<Your Username> ;  Pwd =<Your Password> ; Using TCP port Server =<Your ServerAddress> ;  Port = 1234 ;  Database =<Your DataBase>...
Create and Delete Custom Event Logs Create the Custom Log Use the CreateEventSource method to create your own custom event handler. Before you create the event log, use the SourceExists method to verify that the source you are using does not already exist, and then call CreateEventSource. If you try to create an event log that already exists, a System.ArgumentException error is thrown.  The following code example demonstrates how to create a custom log:        ' Check if the log already exist       If Not EventLog.SourceExists("MyOldSource", System.Environment.MachineName) Then          ' Creating a new log          EventLog.CreateEventSource("MyOldSource", "MyNewLog", System.Environment.MachineName)          Console.WriteLine("New event log created successfully.")       End If You Can Write Event logs on Button ...
System Event Logs You can find the existing logs on a computer by using the GetEventLogs shared method of the EventLog class. The GetEventLogs method searches for all event logs on the local computer, and then it creates an array of EventLog objects that contain the list. The following code example retrieves a list of logs on the local computer, and then displays the names of the logs in a console window: Dim remoteEventLogs() As EventLog 'Gets logs on the local machine, give remote machine name to get the logs on the remote machine       remoteEventLogs = EventLog.GetEventLogs(System.Environment.MachineName)       Console.WriteLine("Number of logs on computer: " & remoteEventLogs.Length)       'Display the list of event logs       Dim log As EventLog       For Each log In remoteEventLogs          Console.WriteLine("Log: " & log.Log)       Nex...
Synchronize Scroll Events of two DataGridView Control Some time there is a need to synchronize scroll event of two or more DataGridView controls, then you can write code like below on first DataGridView Scroll event. Private Sub DataGridView1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll               If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then Exit Sub         If Me.DataGridView2.Rows.Count > 0 And Me.DataGridView1.Rows.Count > 0 Then             Me.DataGridView2.HorizontalScrollingOffset = e.NewValue 'Me.DataGridView1.HorizontalScrollingOffset         End If End Sub and write code displayed below on second DataGridView Scroll event Private Sub DataGridView2_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView2...