Sunteți pe pagina 1din 18

Introduction I am Creating one VB.NET Windows application.

In this Application I am going to explain about Select, Insert, Update and delete Operation in VB.Net and MySQL. Here I am displaying Records in Datagridview from MySQL Database. Note: Before Step 1. I am Creating windows application in vb.net named Myproject. In this application I am using Three Textbox, Datagridview, Four Button controls and Four Labels. I am using Following Textbox Controls and its name : txtUserid.Text: It is used to get the user id from users. txtUserName.Text : It is used to get the user name from users. txtAge.Text : It is used to get the age of the users from users. I am using Following Button Controls and its name : DisplayRecords : if you click the DisplayRecords button, the user details will display in DataGridview. AddRecords : if you click the AddRecords button, the user details will store in data base table. UpdateRecords : if you click the UpdateRecords button,the user details will update from data base table. DeleteRecords : if you click the DeleteRecords button, the user details will delete from data base table. DataGridview1 is used to display user details. If you want to Connect VB.Net with MySQL Data base, You Should use MySql.Data.MySqlClient Namespace. It Contains MySQL Connection class and Methods. MySqlConnection :it is used connect VB.NEt with MySQL Data Base. MySqlCommand : It is used to excute the command MySqlDataAdapter :it is acting as bridge between MySQL and VB.Net. Imports MySql.Data.MySqlClient

MySqlConnection Class is used to connect to MySQL database. Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=mysql;") Diaplay Records in DataGridview from MySQL Database.Display Records in VB.Net from MySQL Database Private Sub DisplayRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=mysql;") Dim sql As MySqlCommand = New MySqlCommand("SELECT * FROM userreg", con) Dim ds As DataSet = New DataSet() Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter() con.Open() DataAdapter1.SelectCommand = sql DataAdapter1.Fill(ds, "Product") DataGridView1.DataSource = ds DataGridView1.DataMember = "Product" con.Close() End Sub Insert Records in MySql Database. Private Sub AddRecords_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles Button2.Click Dim Query As String 'Query = "INSERT INTO userreg" Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=mysql;") 'Dim sql As MySqlCommand = New MySqlCommand(Query, con) Query = "INSERT INTO userreg(idUserReg,UserName, Age)VALUES(" Query = Query + txtUserRegId.Text + ",'" + txtUserName.Text + "'," + txtAge.Text + ")" con.Open() Dim cmd As MySqlCommand = New MySqlCommand(Query, con) Dim i As Integer = cmd.ExecuteNonQuery() If (i > 0) Then lblMsg.Text = "Record is Successfully Inserted" Else lblMsg.Text = "Record is not Inserted" End If con.Close() End Sub

Update Records from MySQL Database. Private Sub UpdateRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim Query As String Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=mysql;") con.Open() Query = "UPDATE userreg SET UserName ='" + txtUserName.Text + "'," Query = Query + "Age = " + txtAge.Text Query = Query + " WHERE idUserReg = " + txtUserRegId.Text

Dim cmd As MySqlCommand = New MySqlCommand(Query, con) MsgBox(Query) Dim i As Integer = cmd.ExecuteNonQuery() If (i > 0) Then lblMsg.Text = "Record is Successfully Updated" Else lblMsg.Text = "Record is not Updated" End If con.Close() End Sub

Delete Records from MySQL Database Private Sub DeleteRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim Query As String Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=test;User ID=root;Password=mysql;") con.Open() Query = "Delete FROM userreg WHERE idUserReg =" + txtUserRegId.Text Dim cmd As MySqlCommand = New MySqlCommand(Query, con) MsgBox(Query) Dim i As Integer = cmd.ExecuteNonQuery() If (i > 0) Then lblMsg.Text = "Record is Successfully Deleted" Else lblMsg.Text = "Record is not Deleted" End If con.Close() End Sub

MySQL Database Here I am creating table in MySQL Data base with Following Fields. The Table Name is userreg. idUserReg : int UserName : Varchar Age : int OutPut

Finally We Create Windows Application in VB.Net with MySQL Database. Here Insert, update, Delete and display Records in grid view.Creating Windows application using MySQL Database Connection in VB.Net if you want to know about How to Upload Excel file in ASP.Net Applications. Please Refer Following Link. Upload Excel file in ASP.Net Applications Thanks for spending your valuable time.if you have any doubts or any Suggestion , Let Me know. I am Happy to update here. Hope you will enjoy this article and provide your valuable suggestion and feedback. Thanks

Happy Coding..... Create Crystal Report from Visual Basic .Net 2008 using Data in DataGridView

Step 1: Prepare data in DataGridView to be placed on CrystalReport

With Me.DataGridView1 .Columns.Add("code", "Code") .Columns.Add("name", "Name") .Columns.Add("gender", "Gender") .Columns.Add("address", "Address")

.EditMode = DataGridViewEditMode.EditProgrammatically .AllowUserToAddRows = False End With

o o o o

'add data to datagridview 'example data in 10 rows For i As Integer = 1 To 0 Me.DataGridView1.Rows.Add() With Me.DataGridView1.Rows(i) .Cells("code").Value = Format(i + 1, "0000") .Cells("name").Value = "Student Name" & i + 1 .Cells("gender").Value = "M" .Cells("address").Value = "PP" End With Next Step 2: Create DataSet and DataTable for Crystal Report Click on Project Menu Click on Add New Item Choose DataSet (Enter DataSet name) Create DataTable in DataSet (Same columns in step 1)

o o

Step 3: Create and Design Crystal Report using Data from DataTable in Step 2 Click on Project Menu Click on Add New Item

o o o o o

Choose Reporting -> Crystal Report Choose Create As Blank Report Right Click on Database Field in Field Explorer of Crystal Report Choose Database Expert Choose Project Data -> ADO.Net DataSets -> (DataSet and DataTable Created in Step 2) OK Drag Each Field in Database Fields into Crystal Report Step 4: Create Form to View Crystal Report using CrystalReportViewer control Create New Form Draw CrystalReportViewer Control on Form Coding Dim dt As New DataTable ' With dt .Columns.Add("code") .Columns.Add("name") .Columns.Add("gender") .Columns.Add("address") End With ' For Each dr As DataGridViewRow In Me.DataGridView1.Rows dt.Rows.Add(dr.Cells("code").Value, dr.Cells("name").Value, _ dr.Cells("gender").Value, dr.Cells("address").Value) Next '

o o o o o

Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument rptDoc = New CrystalReport1 rptDoc.SetDataSource(dt) ' Form2.CrystalReportViewer1.ReportSource = rptDoc Form2.ShowDialog() Form2.Dispose()

Crystal Report is an application which can generate various reports from different data sources, we can create reports, print and preview those report using it, It is compatible with almost a developing platforms and databases, In this article you can find useful resources for generating reports from VB.NET with Crystal Report. We can now see how to create a sample Database and Tables and data. First we have to create a database. Give the database name as "crystaldb" Create a DataBase "crystaldb" In the crystaldb database, let us create three tables OrderMaster, OrderDetails, Product. OrderMaster 1. OrderMaster_id 2. OrderMaster_date 3. OrderMaster_customer 4. OrderMaster_createduser OrderDetails 1. OrderDetails_id 2. OrderDetails_masterid 3. OrderDetails_productid 4. OrderDetails_qty Product 1. Product_id 2. Product_name 3. Product_price Now that you have a database you can start your first VB.NET Crystal Reports. All Crystal Reports programming samples in these tutorials are based upon the following database (crystaldb, the one we created through the previous article). Open Visual Studio .NET and select a new Visual Basic .NET Project.

Create a new Crystal Report for Product table from the above database crystalDB. The Product Table has three fields (Product_id, Product_name, and Product_price). From the main menu in Visual Studio select PROJECT-->Add New Item. Then Add New Item dialogue will appear and select Crystal Reports from the dialogue box.

Accept the default settings and click OK. Next step is to select the appropriate connection to your database. Here we are going to select OLEDB connection for SQL Server Select OLE DB ( ADO ) from Create New Connection.

Select Microsoft OLE DB Provider for SQL Server.

Next screen is the SQL Server authentication screen. Select your Sql Server name, enter userid, password and select your Database Name. Click next, Then the screen shows OLE DB Property values, leave it as it is, and click finish. Then you will get your Server name under OLEDB Connection from there select database name (Crystaldb) and click the tables , then you can see all your tables from your database. From the tables list select Product table to the right side list.

Click Next Button Select all fields from Product table to the right side list.

Click Finish Button. Then you can see the Crystal Reports designer window. You can arrange the design according your requirements. Your screen looks like the following picture.

Now the designing part is over and the next step is to call the created Crystal Reports in VB.NET through Crystal Reports Viewer control. Select the default form (Form1.vb) you created in VB.NET and drag a button and CrystalReportViewercontrol to your form.

Select Form's source code view and put the code on top Imports CrystalDecisions.CrystalReports.Engine Put the following source code in the button click event Imports CrystalDecisions.CrystalReports.Engine Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cryRpt As New ReportDocument

cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") CrystalReportViewer1.ReportSource = cryRpt CrystalReportViewer1.Refresh() End Sub End Class

NOTES: cryRpt.Load ("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") The Crystal Reports is in your project location, there you can see CrystalReport1.rpt. So give the full path name of report here. After you run the source code you will get the report like this.

Hope this article helps you to create your first Crystal Reports.

S-ar putea să vă placă și