110 likes | 275 Views
ສ້າງ DataBase ໃນ SQL Database name:KPIT , Table: TblStd. ສ້າງ Form ໃນ VB.Net.
E N D
ສ້າງ DataBase ໃນ SQL • Database name:KPIT, Table: TblStd
< %@ Import NameSpace="System.Data.SqlClient"%> < script language=“VB” runat="server"> Dim Cnn as SqlConnectionDim Cmm as SqlCommandDim StringConnection as StringDim StringCommand as StringSub Page_Load()If Not Page.IsPostBack() ThenCboSex.Items.Add("Male")CboSex.Items.Add("Female")End IfEnd Sub
Sub BtnSave_Click(s as Object, e as EventArgs) StringConnection="Server=PrumMala;Database=KPIT;User ID=sa;Password=123" Cnn = New SqlConnection(StringConnection) Dim dg as New SqlDataAdapter("Select ID From TblStd where ID="& TxtID.text &"",Cnn) Dim ds as New Dataset dg.Fill(ds,"TblStd") If ds.Tables("TblStd").Rows.Count > 0 Then label2.text="ID is already input, Please Change ID" Else label2.text="" StringCommand="Insert Into TblStd(ID,Name,Sex,DOB,POB,Address,E_mail)" & _ "Values(@ID,@Eng_Name,@Sex,@DOB,@POB,@Address,@E_mail)"
Cmm=New SqlCommand(StringCommand,Cnn) Cmm.Parameters.Add("@ID",TxtID.Text) Cmm.Parameters.Add("@Eng_Name",TxtEName.Text) Cmm.Parameters.Add("@Sex",CboSex.Text) Cmm.Parameters.Add("@DOB",TxtDOB.Text) Cmm.Parameters.Add("@POB",TxtPOB.Text) Cmm.Parameters.Add("@Address",TxtAddress.Text) Cmm.Parameters.Add("@E_mail",TxtEmail.Text) Cnn.Open() Cmm.ExecuteNonQuery() Cnn.Close() Call Clear() End If End Sub
Sub BtnSearch_Click(s as Object, e as EventArgs) Label2.Text="" StringConnection="Server=PrumMala;Database=KPIT;User ID=sa;Password=123" Cnn = New SqlConnection(StringConnection) Dim Cmd as New SqlCommand(" Select * From TblStd " & _ " Where ID=" & TxtID.text & "",Cnn) Cnn.Open() Cmd.ExecuteNonQuery() Dim Rs as SqlDataReader Rs=Cmd.ExecuteReader
Dim B as Integer Do While Rs.Read TxtEName.text=Rs.Item(1) CboSex.DataTextField=Rs.Item(2) TxtDOB.text=Rs.Item(3) TxtPOB.text=Rs.Item(4) TxtAddress.text=Rs.Item(5) TxtEmail.text=Rs.Item(6) B=1 Loop If B< > 1 then Label2.text="Data not found!" End If Cnn.Close() End Sub
Sub BtnUpdate_Click(s as Object, e as EventArgs) StringConnection="Server=PrumMala;Database=KPIT;User ID=sa;Password=123" Cnn = New SqlConnection(StringConnection) Dim Cmd as new SqlCommand("Update TblStd Set " & _ " ID = " & TxtID.Text & _ ",Name=" & TxtEName.Text & _ ",Sex=" & CboSex.text & _ ",DOB="& TxtDOB.Text & _ ",POB=" & TxtPOB.Text & _ ",Address=" & TxtAddress.Text & _ ",E_mail=" & TxtEmail.text & _ " Where ID=" & TxtID.Text & "",Cnn) Cnn.Open() Cmd.ExecuteNonQuery() Cnn.Close() Call Clear() End Sub
Sub BtnClear_Click(s as Object, e as EventArgs) Call Clear() End Sub Sub Clear() Me.TxtID.Text="" Me.TxtEName.Text="" Me.CboSex.ClearSelection() Me.TxtDOB.Text="" Me.TxtPOB.Text="" Me.TxtAddress.Text="" Me.TxtEmail.Text="" End Sub
Add this code into your SAVE button code onclick=" BtnSave_Click" Add this code into your SEARCH button code onclick="BtnSearch_Click Add this code into your UPDATE button code onclick="BtnUpdate_Click" Add this code into your DELETE button code onclick="BtnDelete_Click" Add this code into your CLEAR button code onclick="BtnClear_Click"