Archive

Archive for the ‘WindowsApplication’ Category

Convert Generic List In To DataTable

December 3, 2008 19 comments

Create simple asp.net application with C#. Add new class (GenericToDataTable).

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

public class GenericToDataTable
{
/// <summary>
/// Default Constructor
/// </summary>
private GenericToDataTable()
{ }
/// <summary>
///
/// </summary>
/// <typeparam name=”T”>Custome Class </typeparam>
/// <param name=”lst”>List Of The Custome Class</param>
/// <returns> Return the class datatbl </returns>
public static DataTable ConvertTo<T>(IList<T> lst)
{
//create DataTable Structure
DataTable tbl = CreateTable<T>();
Type entType = typeof(T);

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
//get the list item and add into the list
foreach (T item in lst)
{
DataRow row = tbl.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
tbl.Rows.Add(row);
}

return tbl;
}

/// <summary>
///
/// </summary>
/// <typeparam name=”T”>Custome Class</typeparam>
/// <returns></returns>
public static DataTable CreateTable<T>()
{
//T –> ClassName
Type entType = typeof(T);
//set the datatable name as class name
DataTable tbl = new DataTable(entType.Name);
//get the property list
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
foreach (PropertyDescriptor prop in properties)
{
//add property as column
tbl.Columns.Add(prop.Name, prop.PropertyType);
}
return tbl;
}
}

after add above class compile the code.

after compilation add new class into project (Class Name: clsUser).

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for clsUser
/// </summary>
public class clsUser
{
//create property userid
private int _UserId;
public int UserID
{
get { return _UserId; }
set { _UserId = value; }
}
//create property username
private string _UserName;
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
/// <summary>
/// Default Constructor
/// </summary>
public clsUser()
{ }

public clsUser(int userid,string username)
{
this.UserID = userid;
this.UserName = username;
}
}

After adding this class into project go to Default.aspx page in this page type below code on Page Load event.

//create generic list of class clsUser
System.Collections.Generic.List<clsUser> obj = new
System.Collections.Generic.List<clsUser>();
//add data into list
for (int i = 0; i < 10; i++)
{
obj.Add(new clsUser(i,”a”+i.ToString()));
}
//convert list to datatable
DataTable dt= GenericToDataTable.ConvertTo<clsUser>(obj);

now check the DataTable(dt).

thnx

Create Round Form in C# Application

July 26, 2008 4 comments

1) Create Simple Window Application (C#).

2) in the form set below property.

startposition : CenterScreen
MaximizeBox :False
MinimizeBox :False
Size :
Width :400
Height:400
FormBorderStyle :none

3)  in the form paint event put below code.

private void Form1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Drawing2D.GraphicsPath objGP = new System.Drawing.Drawing2D.GraphicsPath();
objGP.AddEllipse(new Rectangle(0, 0,this.Width, this.Height));
this.Region = new Region(objGP);
this.FormBorderStyle = FormBorderStyle.None;
}
Thnx.

Difference Between Environment.CurrentDirectory And Application.ExecutablePath

May 19, 2008 Leave a comment

here i am using simple windows application.

steps:

1) Create Simple Windows Application using VB.net 2.0.

2) put the button (id=button1) on the form.

3) On the Button Click Event put the below code.

Private Sub Button1_Click(ByVal sender As System.Object, 
               ByVal e As System.EventArgs) Handles Button1.Click
'get the EnvironmentPath
 MsgBox(" EnvironmentPath " & Environment.CurrentDirectory.ToString())
'get the application path
 Dim str As String = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\"))
 MsgBox(" ApplicationPath " & str)
End Sub

4) output of this Application.

EnvironmentPath : Your Application Path + \bin\Debug

ApplicationPath : Your Application Path + \bin\Debug

here both are same.

5) Create a *Set Up Project and install the set up in to your computer.

EnvironmentPath : Your Drive + Documents and Settings\ + UserFolder

ApplicationPath : Your Drive + Documents and Settings + UserFolder + \Start Menu\Programs

the above path is different.

when you use any file or report path and at that time if you use the EnvironmentPath that will give error.

*Set Up Project :

create set up project using the dotnet i give hint ( Open File Menu : Create New Project

–> Select Other Project Types

–> Set Up And Deployment (select Setup Project).

thanks.

Get Connection String from Windows Registry using C#.net

May 15, 2008 6 comments

First of first how to open windows registry editor ?

Open Run Menu (Windows + R) and type regedit the below screen is open.

clip_image002

(Figure 1)

here I am created the simple application using windows C#.net 2.0.

image

steps:

1) Create Simple Windows Application using C#. add the namespace

using Microsoft.Win32;

2) put the below control on the form.

Control Control Id Control Value
label label1 Enter Connection String
label lblGetConnectionString
TextBox txtConnectionString
Button btnSaveConnectionStringInDB Save Connection String In Data
Button btnRetriveConnectionStringFromRegistry Retrive Connection String From Windows Registry

3) in the Figure 1 Local Machine Contain Five Key

1) Hardware 2) Sam 3) Security 4) Software 5) System.

in the figure 1 only one key is display. now we code and then new key is added under the

HKEY_LOCAL_MACHINE. in below screen the brief idea is given.

image

4) Now Put the Below code in to .CS File.

4.1) Declare the global variable

//configuration data for the local machine
   //Windows registry base key HKEY_LOCAL_MACHINE
   RegistryKey objRegistryKey = Registry.LocalMachine;
4.2) on the button click (btnSaveConnectionStringInDB) put below code.
private void btnSaveConnectionStringInDB_Click(object sender, EventArgs e)
   {
     try
     {
       //now set the new key under the base key
       //argument 1) Name Of The Key (MyConnectionString)
       //         2) Key (TextBox Enter Value)
       //         3) value type (string)
       //now i am use hardcoded key value
       objRegistryKey.SetValue("MyConnectionString", txtConnectionString.Text, RegistryValueKind.String);
       MessageBox.Show("Key Is Sucessfully Registered");
     }
     catch (Exception ex)
     {
       MessageBox.Show("Can Not Store Data In Registry" + ex.Message.ToString());
     }
   }

4.3) on the button click (btnRetriveConnectionStringFromRegistry) put below code.

private void btnRetriveConnectionStringFromRegistry_Click(object sender, EventArgs e)
{
  string values = (string)objRegistryKey.GetValue("MyConnectionString");
  //display the connection string value in the label
  lblGetConnectionString.Text = values;
}

5) Add Key In to Windows Registry.

image

After the get message open the windows registry.

image

entered key is added successfully please check the figure1 and last this figure.

6) Get the Value from the RegistryKey (MyConnectionString)

image

Thnx

Follow

Get every new post delivered to your Inbox.

Join 244 other followers