Wednesday, 7 March 2012

Database connection and perform insert query operation on database in .net 3.5


Now we have seen how to generate connection string in .net.Now will see how to use that connection string to connect to database.Here we will see insert query database operation:

1)Submitting data to database

    1. Go to file menu create new website.Go to design page as shown below:



2.Create  form as shown below using toolbox:

Now go to source code by clicking Source tab as shown below and type code highlighted below in between dropdown tag.Following highlighted code is used to insert value to dropdown control:

Now another way of inserting value to dropdown box is given below .just follow the screnshot given below:
    a)Click on smart tag of dropdown button:

    b)Now click on Edit Items which opens a following window:

c)Now click on Add button.
 d)Now move to Text and type please select as shown below:

e)Now click on Add button this will add "Please select" to your dropdown button.You can check it in left side of window as shown below:

 Repeat above steps d) and e) to enter male and female value to dropdown button.

 Remove button is used to remove element from dropdown button.
3)Now add sql datasource control to your page as shown below:
Now we have seen the steps for creating connection string.If you forgot please go to Given link.Steps to generate connection string
 Follow the steps given in above link,create one database then create table with attribute name Name ,Roll no.,Gender and City.Keep datatype for all attribute as nvarchar(50). Here you will get connection string which will be used to connect to your database.



After creating table ,databse and  generating connection string double click on Submit button present on .aspx page.This will open .aspx.vb page as shown below:



Now whenever we will do any code related with database  we need to type following two import statement at top of your .aspx.vb page.
Imports System.Data
Imports System.Data.SqlClient

Then type following insert query code into your submit button click event:
 Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("a").ToString())
        Dim sqlqry As String = "insert into Table1(Name,Rollno,Gender,City) values(' " & txt1.Text & " ',' " & txt2.Text & " ',' " & dd1.SelectedValue & " ',' " & txt3.Text & " ')"
        Dim cmd As SqlCommand
        Try
            connection.Open()
            cmd = New SqlCommand(sqlqry, connection)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
           lblmsg.Text = ex.Message
        Finally
            connection.Close()
        End Try
 In above code "a" is your connection string.txt1,txt2,txt3 and dd1 are ID of your control.Table1 is name of your table.



In above code we have used try catch so it will throw exception if something wrong happens.In order to display or to know which types of exception you are getting take one label control on .apsx page and set prperties as shown below:

Now save and run your website:


 Now how to view data in database?Go to view click on database explorer and follow the given screenshot:





No comments:

Post a Comment