Dim objcon As New SqlConnection(ConfigurationManager.ConnectionStrings("student").ConnectionString) Dim objadp As SqlDataAdapter Dim ds As Data.DataSet Dim cmd As SqlCommand
Public Function selectdata(ByVal qry As String) As DataSet
objcon.Open() objadp = New SqlDataAdapter(qry, objcon) ds = New Data.DataSet objadp.Fill(ds) objcon.Close() Return ds
End Function
Public Function qryexe(ByVal qry As String) As Boolean objcon.Open() cmd = New SqlCommand(qry, objcon) Dim i As Integer = cmd.ExecuteNonQuery() objcon.Close() If i = 0 Then Return False Else Return True End If
End Function End Class ====================Default.aspx.vb========================
Partial Class _Default Inherits System.Web.UI.Page
Dim qry As String Dim ds As New System.Data.DataSet Dim obj As New Class1 Shared flag As Boolean = False Shared chk As Boolean
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click chk = True End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click chk = False End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click qry = "delete from stud where name='" + DropDownList1.SelectedItem.Text + "'" obj.qryexe(qry) flag = False Page_Load(sender, e) trno.Text = "" tname.Text = "" tresult.Text = "" End Sub
Public Sub display() qry = "select * from stud where name='" + DropDownList1.SelectedItem.Text + "'" ds = obj.selectdata(qry) trno.Text = ds.Tables(0).Rows(0).Item("rno").ToString() tname.Text = ds.Tables(0).Rows(0).Item("name") tresult.Text = ds.Tables(0).Rows(0).Item("result") End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged ds.Clear() display() End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If flag = False Then ds.Clear() qry = "select * from stud" ds = obj.selectdata(qry) DropDownList1.DataSource = ds DropDownList1.DataTextField = ds.Tables(0).Columns("name").ToString() DropDownList1.DataValueField = ds.Tables(0).Columns("rno").ToString() DropDownList1.DataBind()
GridView1.DataSource = ds GridView1.DataBind()
flag = True End If End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click If chk = True Then qry = "insert into stud(name,result) values('" + tname.Text.Trim + "','" + tresult.Text.Trim + "')" obj.qryexe(qry) Else qry = "update stud set name='" + tname.Text.Trim + "',result='" + tresult.Text.Trim + "' where rno=" + trno.Text.Trim obj.qryexe(qry) End If flag = False Page_Load(sender, e) trno.Text = "" tname.Text = "" tresult.Text = "" End Sub