Thursday, January 28, 2010

Passing data from one database to another database table (Access) (C#)

string conString = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=Backup.mdb;Jet
OLEDB:Database Password=12345";
OleDbConnection dbconn = new OleDbConnection();
OleDbDataAdapter dAdapter = new OleDbDataAdapter();
OleDbCommand dbcommand = new OleDbCommand();

try
{
if (dbconn.State == ConnectionState.Closed)
dbconn.Open();

string selQuery = "INSERT INTO [Master] SELECT * FROM [MS Access;DATABASE="+
"\\Data.mdb" + ";].[Master]";

dbcommand.CommandText = selQuery;
dbcommand.CommandType = CommandType.Text;
dbcommand.Connection = dbconn;
int result = dbcommand.ExecuteNonQuery();
}
catch(Exception ex) {}

How to create an Access database by using ADOX and Visual C# .NET

Build an Access Database

1. Open a new Visual C# .NET console application.
2. In Solution Explorer, right-click the References node and select Add Reference.
3. On the COM tab, select Microsoft ADO Ext. 2.7 for DDL and Security, click Select to add it to the Selected Components, and then click OK.
4. Delete all of the code from the code window for Class1.cs.
5. Paste the following code into the code window:

using System;
using ADOX;

private void btnCreate_Click(object sender, EventArgs e)
{
ADOX.CatalogClass cat = new ADOX.CatalogClass();

cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source=D:\\NewMDB.mdb;" +"Jet OLEDB:Engine Type=5");
MessageBox.Show("Database Created Successfully");
cat = null;
}

Wednesday, January 27, 2010

Pager Control for Repeater, DataList

http://www.codeproject.com/KB/custom-controls/CollectionPager.aspx?fid=160845&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=251

Friday, January 22, 2010

How to use Crystal Report into the ASP.NET

using CrystalDecisions.CrystalReports.Engine;
protected void Page_Load(object sender, EventArgs e)
{
ConfigureReport();
}
private void ConfigureReport()
{
ReportDocument mydoc = new ReportDocument();
string reportPath = Server.MapPath("Report.rpt");
mydoc.Load(reportPath);
CrystalReportViewer1.ReportSource = mydoc;
CrystalReportViewer1.DataBind();
}

Disable Typing in Combobox in C# window application

//Disable Typing in Combobox in C# window application
Set the Combobox DropDownStyle property to : DropDownList

How to clear all the textbox/groupbox on single click

public void ClearTextBox(Form f)
{
for (int i = 0; i < f.Controls.Count; i++)
{
if (f.Controls[i] is TextBox)
{
f.Controls[i].Text = "";
}
else if (f.Controls[i] is GroupBox)
{
for (int j = 0; j < f.Controls[i].Controls.Count; j++)
{
if (f.Controls[i].Controls[j] is TextBox)
{
f.Controls[i].Controls[j].Text = "";
}
}
}
}
}
//How to call this function(Into the Submit Button)
Make a object from the Program.cs file.
object.ClearTextBox(this);
//After submit query

Monday, January 18, 2010

Window Application : C# Function for Insert,get data and fill the dropdown list

//To get data from database
string constr;
constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\database.mdb";
public object getdata(string str)
{
OleDbConnection con = new OleDbConnection(constr);
con.Open();
object a;
OleDbCommand cmd = new OleDbCommand(str, con);
a = cmd.ExecuteScalar();
con.Close();
return a;
}
//To inserting data into the database
public void insertdata(string str)
{
OleDbConnection con = new OleDbConnection(constr);
con.Open();
OleDbCommand cmd = new OleDbCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
}
//To fill the dropdownlist
public void dataset(ComboBox ddl, string str)
{
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter dap = new OleDbDataAdapter(str, con);
con.Open();

DataSet ds = new DataSet();
dap.Fill(ds);

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i].ItemArray[0].ToString()))
{
ddl.Items.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
}
}
con.Close();
}

Monday, January 4, 2010

URL validation in C# window application

private void TextBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rURL = new System.Text.RegularExpressions.Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
if (txtURL.Text.Length > 0)
{
if (!rURL.IsMatch(txtEmail.Text))
{
MessageBox.Show("Please provide valid URL", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtURL.SelectAll();
}
}
}

Email validation in C# window application

private void TextBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rEmail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (txtEmail.Text.Length > 0)
{
if(!rEmail.IsMatch(txtEmail.Text))
{
MessageBox.Show("Please provide valid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtEmail.SelectAll();
}
}
}

Validation for textbox : C# window application

private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 47 && e.KeyChar <= 58) || (e.KeyChar == 46) || (e.KeyChar == 8))
{
e.Handled = false;
}
else
{
e.Handled = true;
MessageBox.Show("Please enter only number");
}
}