Search

Sunday, February 28, 2010

Update or Edit the records of the database

I have use MS ACCESS database to perform updation. All i had did is take name to find the record from the database and then fired update query.

1) Import files

Imports System
Imports System.Data
Imports System.Data.OleDb

2) Declare variables and objects

Dim strConn As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("~/App_Data/db2.mdb") & ";"
Dim conn As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim rdr As OleDb.OleDbDataReader
Dim strsql As String

3) In update button write down

Dim ssql As String = "UPDATE Stud SET Name = '" + TextBox2.Text + "' where Rollno='" + TextBox1.Text + "';"
MsgBox(ssql)
conn.Open()
Dim comm As New OleDbCommand(ssql, conn)
comm.ExecuteNonQuery()
MsgBox("record is Updated")
Response.Redirect("~/Default.aspx")
conn.Close()

Delete records form the datatbase using asp.net

A simple code to delete the records from the MS ACCESS database. I have use a textbox to get the id or name of the record to delete and a delete button to perform deletion.

1)Import files

Imports System
Imports System.Data
Imports System.Data.OleDb

2)Declare variable and objects

Dim strConn As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("~/App_Data/db2.mdb") & ";"
Dim conn As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim rdr As OleDb.OleDbDataReader
Dim strsql As String

2)In delete button write down

Dim ssql As String = "delete from stud where (Rollno ='" & TextBox1.Text & "')"
MsgBox(ssql)
conn.Open()
Dim comm As New OleDbCommand(ssql, conn)
comm.ExecuteNonQuery()
MsgBox("record is delete")
Response.Redirect("~/Default.aspx")
conn.Close()

Thats it.......

Insert data into database in asp.net

Friends here i am showing how to insert data into MS ACCESS database. I have use some textboxes to get the values ftom the users and a button to insert the data.

1) Imports the files
Imports System
Imports System.Data
Imports System.Data.OleDb

2) Declare variables an objects

Dim strConn As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("~/App_Data/db2.mdb") & ";"
Dim conn As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim rdr As OleDb.OleDbDataReader
Dim strsql As String
Dim strins As String

3) In insert button

strins = "INSERT INTO stud VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "')"
cmd = New OleDb.OleDbCommand(strins, conn)
conn.Open()
cmd.ExecuteScalar()
conn.Close()
MsgBox("Record sudmited")
Response.Redirect("~/Default.aspx")


Thats it simple.............

Making Connection with access database in asp.net

I have made an another simple program to make connectivity with MS ACCESS. I have use gridview to display my data.


1) Import this files:

Imports System
Imports System.Data
Imports System.Data.OleDb

2) Make decleration of variables and objects:

Dim strConn As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("~/App_Data/db2.mdb") & ";"
Dim conn As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim rdr As OleDb.OleDbDataReader
Dim strsql As String

3)In page load event write down:

conn = New OleDb.OleDbConnection(strConn)
conn.Open()
strsql = "select * from stud"
cmd = New OleDb.OleDbCommand(strsql, conn)
rdr = cmd.ExecuteReader()
GridView1.DataSource = rdr
GridView1.DataBind()
rdr.Close()
conn.Close()

Create an Login Page in asp.net

Hi guys i have created a simple login sample using custom login codes. Here i have use session to created a session of the users. I have also taken the database to access username and password.

The code behind login button is:

Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Dim DBConn as OleDbConnection
Dim DBCommand As OleDbDataAdapter
Dim DSLogin as New DataSet
DBConn = New OleDbConnection("PROVIDER=" _
& "Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& Server.MapPath("LogIn.mdb;"))
DBCommand = New OleDbDataAdapter _
("Select UserID from " _
& "Users Where " _
& "UserName = '" & txtUserName.Text _
& "' and Password = '" & txtPassword.Text _
& "'", DBConn)
DBCommand.Fill(DSLogin, _
"UserInfo")
If DSLogin.Tables("UserInfo"). _
Rows.Count = 0 Then
lblMessage.Text = "The user name and password " _
& "were not found. Please try again."
Else
Session("UserID") = DSLogin.Tables("UserInfo"). _
Rows(0).Item("UserID")
Session("UserName") = txtUserName.Text
Response.Redirect("./welcome.aspx")
End If

The Code for logout button will be:

Session.Removeall()
Response.Redirect("login.aspx")


There are various ways available to do login but this is the simplest form of it.