Monday, March 26th, 2012 | Author:
admin
Exit Application
C# System.Environment.Exit(0);
Load/Show a Form
Form2 myForm = new Form2();
myform.show();
currentform.hide();
Change Startup Form
Open Program.cs
Application.Run(new Form2());
DateTimePicker Control
string student_date_of_birth = date_of_birth.Value.ToString(“yyyy-MM-dd”);
//output 2012/04/27
Connection with PostGreSQL
hastTable(simple) Example:
Link: http://pastebin.com/j6LTHmt1 (A complete example of create hastable, assign values, search by key, if not found then add that key into hastable, if found then increment value by 1)
Create Hashtable and Assign Values
Hashtable hashtable = new Hashtable();
hashtable.Add(“Blaze”, 400);
hashtable.Add(“Fiery”, 600);
Add New Key with Value
hashtable.Add(search_me, 1);
Traverse Hashtable values
foreach (int val in hashtable.Values) //hashtable.Keys
MessageBox.Show(val.ToString());
Increase value by 1
hashtable[search_me] = (int)hashtable[search_me] + 1;
hastTable Example with struct type:
struct structMultipleValues
{
public int TotalNoOfAccount;
public double TotalBalance;
//public double LastValue;
};
structMultipleValues hTblVal;
hTblVal.TotalNoOfAccount = 1;
hTblVal.TotalBalance = Convert.ToDouble(words[1]);
hashTable.Add(productCode.ToString(), hTblVal); //create an array element
//traverse hashtable.
foreach (DictionaryEntry gg in hashTable)
{
structMultipleValues tmp = (structMultipleValues)gg.Value;
gg.Key, tmp.TotalNoOfAccount, tmp.TotalBalance; //values
}
DataGridView Example:
//Read user_info Table and Assign value to DataGrid
SqlConnection myConnection = Program.dbConn;
using (SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(“SELECT * FROM user_info”, myConnection))
{
// Use DataAdapter to fill DataTable
DataTable myDataTable = new DataTable();
mySqlDataAdapter.Fill(myDataTable);
// Render data onto the screen
dataGridView1.DataSource = myDataTable;
}
Read .htm file using C#
string story;
StreamReader corpusPage = new StreamReader(“jbgl_02-05-2012.htm”, Encoding.Default);
story = string.Empty;
string s = “”;
int total_line = 0;
while ((story = corpusPage.ReadLine()) != null)
{
total_line++;
s = s + story.ToString();
//MessageBox.Show(story.ToString());
if ((story == ” “) || (story == “”))
{
continue;
}
}
richTextBox1.Text = s;
Load Combo Box in C#
//Load product category combo box
combo_product_cat_id.DataSource = myDAL.GetRS(“select id, product_category_name from product_category”).Tables[0].DefaultView;
combo_product_cat_id.DisplayMember = “product_category_name”;
combo_product_cat_id.ValueMember = “id”;
combo_product_cat_id.DropDownStyle = ComboBoxStyle.DropDownList;
DataTable: Display Database Table Data using DataTable(DB: PostGreSql)
DataTable myDataTable = myDAL.GetRS(“select * from product_info where id=” + product_id).Tables[0];
DataView myDataView = myDataTable.DefaultView;
for (int j = 0; j < myDataView.Count; j++)
{
strTblData += (j+1)+” “+myDataView[j][0].ToString()+” (“+myDataView[j][1].ToString()+”) \n”;
}
label2.Text = “Table Name: product_info \n Total Data Rows Found: ” + total_row + “\n All Data:” + strTblData;
DataTable: Display Database Table Data using DataTable(DB: SQL Server 2000)
Program.db_connection_sql_server(); //Create Database Connection
SqlConnection myConnection = Program.dbConn;
String sqlSelectTbls = “select * from interest_provision”;
SqlCommand myCommand = new SqlCommand(sqlSelectTbls, myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.HasRows)
{
while (myReader.Read())
{
MessageBox(myReader["RecordCount"].ToString());
}
}
//in program.cs file
public static void db_connection_sql_server()
{
dbConn = new SqlConnection(“Data Source=ADMIN-ITSD;Initial Catalog=db_name;User ID=sa;Password=pass123;”);
dbConn.Open(); //MessageBox.Show(“Connection created.”);
}