Sei sulla pagina 1di 13

.

Net (C#)
With visual studio 2008

Create the app.config file in c# project ................................................................................................................ 3 Access database ........................................................................................................................................................ 3 Insert Data to the Database .................................................................................................................................... 3 Update the Database................................................................................................................................................ 4 Delete data of the Database ................................................................................................................................... 5 Combo-box fill ........................................................................................................................................................... 5 RowHeaderMouseClick-Data Grid View .............................................................................................................. 5 What is Crystal Reports? ......................................................................................................................................... 7 Crystal Report with DataSet and DataTable using C# ....................................................................................... 7 Step 1: Adding A DataSet and DataTable ................................................................................................................ 7 Step 2: Adding A Crystal Report ............................................................................................................................... 9 Step 3: Binding Our Report to our DataSource ....................................................................................................12

Create the app.config file in c# project


Right click on visual studio c# project and add new class called Application Configuration Fileapp.config Add following lines to that file.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ConnectionString" value="Driver={MySQL ODBC 5.1 Driver};Server=127.0.0.1;Database=companysalesstock;uid=root;Password=1234;" /> </appSettings> </configuration>

Access database
Add following references to the class.
using System.Data.Odbc; using System.Configuration; -----------------------------------------------------------------------------

class User_Account { OdbcConnection con; OdbcCommand cmd; public User_Account() { String n=ConfigurationSettings.AppSettings["ConnectionString"].ToString(); } }

Insert Data to the Database


private void btnInsert_Click(object sender, EventArgs e){ OdbcCommand cmd = new OdbcCommand(); cmd.Connection = con; if (con.State.ToString() == "Closed") { con.Open(); } string query = "insert into testing (id,name) values ('" + txtId.Text+ "','" + txtName.Text + "')"; cmd.CommandText = query; cmd.ExecuteNonQuery(); con.Close(); }

Fill DataGridView
public DataTable fillGrid(string query) { con.Open(); OdbcCommand cmds = con.CreateCommand(); cmds.CommandText = query; OdbcDataAdapter da = new OdbcDataAdapter(cmds); DataTable dt = new DataTable(); da.Fill(dt); con.Close(); return dt; } private void Form1_Load(object sender, EventArgs e) { dgvInsert.DataSource = fillGrid("select * from testing"); dgvInsert.Refresh(); }

Update the Database


private void btnUpdate_Click(object sender, EventArgs e) { con.Open(); OdbcCommand cmd = new OdbcCommand(); cmd.Connection = con; string query = "update testing set name='" + txtName.Text + "' where id='" + txtId.Text + "'"; cmd.CommandText = query; cmd.ExecuteNonQuery(); MessageBox.Show("update Suceesfull"); con.Close(); Form1_Load(null, null); }

Delete data of the Database


private void btnDelete_Click(object sender, EventArgs e) { OdbcCommand cmd = new OdbcCommand(); cmd.Connection = con; con.Open(); string query = "delete from testing where id='" + txtId.Text + "'"; cmd.CommandText = query; cmd.ExecuteNonQuery(); MessageBox.Show("Delete Suceesfull"); con.Close(); }

Combo-box fill
public DataTable fillGrid(string query) { con.Open(); OdbcCommand cmds = con.CreateCommand(); cmds.CommandText = query; OdbcDataAdapter da = new OdbcDataAdapter(cmds); DataTable dt = new DataTable(); da.Fill(dt); con.Close(); return dt; }

private void Form1_Load(object sender, EventArgs e) { comboBox.DataSource = fillGrid("select name from testing"); comboBox.DisplayMember = "name"; }

RowHeaderMouseClick-Data Grid View


private void dgvInsert_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{ txtId.Text = dgvInsert.SelectedRows[0].Cells[0].Value.ToString(); txtName.Text = dgvInsert.SelectedRows[0].Cells[1].Value.ToString(); }

Search with Radio Button


public DataTable fillGrid(string query) { con.Open(); OdbcCommand cmds = con.CreateCommand(); cmds.CommandText = query; OdbcDataAdapter da = new OdbcDataAdapter(cmds); DataTable dt = new DataTable(); da.Fill(dt); con.Close(); return dt; }

private void btnSearch_Click_1(object sender, EventArgs e) { if (rbSeachbyName.Checked.Equals(true)) { dgvInsert.DataSource = fillGrid(" select * from testing where name='" + txtSearchName.Text + "'"); } }

Loging Fuction
private void btnLogin_Click(object sender, EventArgs e) { con.Open(); cmd = new OdbcCommand(); cmd.Connection = con; string query2 = "select user_name,password from useraccount where user_name ='" + txtUserName.Text + "' AND password ='" + txtPassword.Text + "'"; cmd.CommandText = query2; OdbcDataReader rdr; rdr = cmd.ExecuteReader(); while (rdr.Read()) { if (rdr["user_name"].ToString() == txtUserName.Text && rdr["password"].ToString() == txtPassword.Text) { Form2 f2 = new Form2(); f2.Show(); 6

this.Visible = false; } } con.Close(); }

What is Crystal Reports?


With Crystal Reports for Visual Studio 2005, you can create complex and professional reports in a GUI-based program. Then you can connect your report to almost any database source, as well as to proxy data, such as a result set (for example, an ADO.NET DataSet).

Crystal Report with DataSet and DataTable using C#


This article is demonstrating how to create a Crystal Report with ADO.NETDataTable. Then create a form as frmMain and add a Crystal report viewer control to this form. Add a Reference to System.Data.Odbc.

Step 1: Adding ADataSet and DataTable


Add a DataSet to your Project and name it as myDataSet, as follows

Now add a DataTable to myDataSet

Now add columns to your DataTable as given below in the image. Your column name and datatype should be the same as that in your database.

Change the datatype of the DataTable columns as shown below

Now save this DataTable as my_dt

Now we have created our DataSet and DataTable. The next step is to create a CrystalReport.

Step 2: Adding A Crystal Report


Add a Crystal report to the project and name it as my_rpt.

Choose Using the report wizard.

Choose the data source from project data which is my_dataset in our case.

10

Choose the columns to be displayed on the report.

Add a group by column, if you want.

Now our designing part is complete.

11

Step 3: Binding Our Report to our DataSource


Below is the C# code to bind our report to the datasource.
using System.Data.Odbc; using System.Configuration; namespaceCrystalReportWithODBC { public partial class frmMain : Form { OdbcConnection con; publicfrmMain() { InitializeComponent(); string n = ConfigurationSettings.AppSettings["connectionString"].ToString(); con = new OdbcConnection(n); OdbcCommand cmd = new OdbcCommand(); cmd.Connection = con; } private void frmMain_Load(object sender, EventArgs e) { my_rptobjRpt; // Creating object of our report. objRpt = new my_rpt(); String con = "Driver={MySQL ODBC 5.1 Driver};Server=127.0.0.1; Database=companysalesstock;uid=root;Password=1234;"; OdbcConnection myCon = new OdbcConnection(); String Query1 = "select a.PROJECT_ID,a.PROJECT_NAME,b.GROUP_NAME from tbl_projecta,tbl_project_group b where a.group_code= b.group_code"; OdbcDataAdapter adapter = new OdbcDataAdapter(Query1, con); DataSet Ds = new DataSet() // here my_dt is the name of the DataTable which we // created in the designer view. adapter.Fill(Ds, "my_dt"); if (Ds.Tables[0].Rows.Count == 0) { MessageBox.Show("No data Found", "CrystalReportWithODBC"); return; } // Setting data source of our report object objRpt.SetDataSource(Ds);

12

CrystalDecisions.CrystalReports.Engine.TextObject root; root = (CrystalDecisions.CrystalReports.Engine.TextObject) objRpt.ReportDefinition.ReportObjects["txt_header"]; root.Text = "Sample Report By Using Data Table!!"; // Binding the crystalReportViewer with our report object. crystalReportViewer1.ReportSource = objRpt; } } }

http://www.youtube.com/playlist?list=PLS1QulWo1RIZrmdggzEKbhnfvCMHtT-sA

13

Potrebbero piacerti anche