Connection between Visual Studio 2008 and SQL Server

Open visual studio 2008/2010. Right click project->Add->Class
name it dbconn.cs
dbconn.cs code should looks like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;

namespace AnalyzeFlora_VS2008
{
static class dbconn
{
public static SqlConnection dbConnection;
public static void connectSQLServer()
{
dbConnection = new SqlConnection(“Data Source=ADMIN-ITSD;Initial Catalog=Florabank_online;User ID=sa;Password=fsbank;”);
dbConnection.Open();
}

public static void db_conn_status()
{

MessageBox.Show(dbConnection.State.ToString());
//return dbConnection.State.ToString();
}

}
}

Now in any form call dbconn.connectSQLServer in form load. Full code of form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
dbconn.connectSQLServer(); //Create Connection with SQL Server 2000
SqlConnection dbConnection = dbconn.dbConnection;  //MessageBox.Show(dbConnection.State.ToString());
}
}

SQL Query for SQL server 2000

List of all the user created tables from the sql server database,
SELECT name FROM sysobjects WHERE xtype = ‘U’;

List field names of the database table,
SELECT [name] AS [Column name] FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = ‘U’ AND [NAME] = ‘your table name’)

 

CRUD Application using C# and PostGreSQL

Connect with PostGreSQL:
static string serverName = “127.0.0.1″; //localhost
static string port = “5432″; // default port
static string userName = “postgres”; //admin name
static string password = “abc123″; //admin password
static string databaseName = “right_click”; //database name
string connString = String.Format(“Server={0};Port={1};User Id={2};Password={3};Database={4};”,
serverName, port, userName, password, databaseName);
NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString);

Auto Increment Column in PostGRESql
data type SERIAL

Insert Data into PostGre Table
string insertCommand = String.Format(sql_command);
NpgsqlCommand pgsqlcommand = new NpgsqlCommand(insertCommand, pgsqlConnection);
pgsqlcommand.ExecuteNonQuery();

http://dl.dropbox.com/u/1403404/C%23Projects/crud.rar

Display data into DataGrid
pgsql_conn_bl bl = new pgsql_conn_bl(); //pgsql_conn_bl is a class where connection and crud functions reside.
dataGridView1.DataSource=bl.GetAllRecords(“user_info”);
and in GetAllRecords(string table_name) function:
DataTable dtRecord = new DataTable();
NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString);//connection with postgre
pgsqlConnection.Open();//open connection
string selectCommand = “Select * from ” + table_name;
NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(selectCommand, pgsqlConnection);
Adpt.Fill(dtRecord);//Fill data grid.

Click here to download full project.

Connection between Visual Studio 2010 and PostGre

Few parameters of postgreSQL(You set these during PostGreSQL Installation):
serverName = “127.0.0.1″; //localhost
port = “5432″; // default port
userName = “postgres”; //admin name
password = “abc123″; //admin password
databaseName = “TestDB”; //database name

Now Connection:
PostGreSQL is an open source database which require additional files to connect with microsoft visual studio 2010 or 2008 (i am not sure about older versions).

  1. Open a project in Visual Basic C# example name pgsql_conn
  2. Download driver files from here. Extract into project root folder. i.e pgsql_conn\Npgsql2 (where de, es… etc folder and Mono.Security.dll, Npgsql.dll …etc files reside)
  3. In solution explorer of visual studio 2010/2008 right click Reference->Add Reference. Browse Tab-> Select Npgsql2\Mono.Security.dll & Npgsql.dll
  4. Mono.Security.dll & Npgsql.dll added in reference successfully.
  5. Right click on project name (pgsql_conn) in solution explorer. -> Add-> Class -> enter file name pgsql_conn_bl.cs(or any).
  6. Open pgsql_conn_bl.cs class and write code as show.
  7. Now use this line of code pgsql_conn_bl x = new pgsql_conn_bl(); where you want to make connection with postgresql.

That’s all about connection.

 

c# dot net sample code

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.”);
}

Passport Size Photo

Standard Photo Size(Passport Size):

Width: 1.8 inc
Height: 2 inc

SRC: “Nothing to print” error display for Adhoq Query->Last 2 Menus

Software: SRCProblem: “Nothing to print” error display for Adhoq  Query->Last 2 Menus

Solution:
01. Find BranchID from Branch Table about this Branch

02. Run the following script on Query Analyzer
03. Replace XXX with Respective Branch ID not Branch Code
truncate table AttachBranch;
update TTItem set ParentBranchID=’XXX’;

Software needed for a Fresh Computer

The following software need to install for a new/fresh computer install

  1. Windows XP/Windows 7
  2. Office
  3. Winrar
  4. Dreamweaver, PHP Designer
  5. Flash FXP
  6. Wamp
  7. Pidgin
  8. Firefox
  9. Firefox Extension (fission, Xmark)
  10. Avro (bangla typing software)
  11. Flash player
  12. KM Player (video player)

Protected: To reset Terminal ID follow the instruction

This post is password protected. To view it please enter your password below:


Category: Janata Bank Ltd  Enter your password to view comments.

javascript show/hide a span/div on click

onclick=”Javascript: show_hide(‘district_list’,'block’);show_hide(‘country_list’,'hide’);”

Add a java script file as: <script src=”js/custom_function.js”></script>
[Now the javascript File]
function show_hide(id, action) {
if (action==”hide”) {
document.getElementById(id).style.display = “none”;
} else {
document.getElementById(id).style.display = “block”;
}
}