Saturday, 14 November 2015

Simple Insert,Update,Delete Queries and Bind To Gridview,Dropdownlist Using VB.NET

Simple Insert,Update,Delete Queries and Bind To Gridview,Dropdownlist


Create Simple Insert,Update,Delete,Select Query and Bind To Gridview,Dropdownlist Using in VB.Net 

                                DEMO


                             Download

                         HTML Coding

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Simple Insert,Update,Delete Queries and Bind To Gridview,Dropdownlist Using VB.NET</title>   
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">         
        <table> 
            <tr><td>
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
                </td><td>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td><td></td></tr>
            <tr><td>
        <asp:Label ID="Label2" runat="server" Text="Number"></asp:Label>
                </td><td><asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
                </td><td></td></tr>
            <tr><td>Image</td><td>
        <asp:FileUpload ID="FileUpload1" runat="server" />
                </td><td></td></tr>
            <tr><td class="auto-style1"></td><td class="auto-style1">
        <asp:Button ID="Button1" runat="server" Text="Register" />
<asp:Button ID="Button2" runat="server" Text="Update" />
<asp:Button ID="Button3" runat="server" Text="Delete" />
<asp:Button ID="Button4" runat="server" Text="Select" />
   
                </td><td class="auto-style1"></td></tr>
            <tr><td>&nbsp;Username </td><td>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
   
                </td><td></td></tr>
            <tr><td></td><td>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
          <asp:ButtonField DataTextField="name" HeaderText="User Name" />
                <asp:ButtonField DataTextField="number" HeaderText="Number" Text="Button" />
                <asp:ImageField ControlStyle-Height="200" DataImageUrlField="imgpath" HeaderText="Image">
<ControlStyle Height="200px"></ControlStyle>
                </asp:ImageField>
            </Columns>
        </asp:GridView>
                </td></tr>
        </table> 

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbcon %>" SelectCommand="SELECT * FROM [reg]"></asp:SqlDataSource>
       
</div>
    </form>
</body>

</html>

                             
                           C# Coding


Imports System.Data
Imports System.Data.SqlClient


Partial Class _Default
    Inherits System.Web.UI.Page

    Dim con As SqlConnection = New SqlConnection()
    ' Database Connection String
    Public Sub dbconn()
        con = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("dbcon").ConnectionString)
        con.Open()
    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgsHandles Me.Load
        BindDropDown()
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgsHandles Button1.Click
        ' Insert Query with Image Upload
        dbconn()
        Dim imgname As String
        imgname = FileUpload1.FileName
        FileUpload1.SaveAs(Server.MapPath("file/" + imgname))

        Dim cmd As SqlCommand = New SqlCommand("insert into reg values('" + txtName.Text + "','" + txtNumber.Text + "','" + imgname + "','" + "file/" + imgname + "')", con)
        cmd.ExecuteNonQuery()
        Response.Write("<script>alert('Data Registered')</script>")
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgsHandles Button2.Click
        'Update query

        dbconn()
        Dim cmd As SqlCommand = New SqlCommand("update reg set number= '" + txtNumber.Text + "' where name='" + txtName.Text + "'", con)
        cmd.ExecuteNonQuery()
        Response.Write("<script>alert('Data Updated')</script>")

    End Sub

    Protected Sub Button3_Click(sender As Object, e As EventArgsHandles Button3.Click
        ' Delete Query
        dbconn()
        Dim cmd As SqlCommand = New SqlCommand("delete from reg where name='" + txtName.Text + "'", con)
        cmd.ExecuteNonQuery()
        Response.Write("<script>alert('Data Deleted')</script>")
    End Sub

    Protected Sub Button4_Click(sender As Object, e As EventArgsHandles Button4.Click
        ' reg table Bind To GridView
        dbconn()
        Dim cmd As SqlCommand = New SqlCommand("select * from reg", con)
        Dim adp As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        adp.Fill(dt)
        Dim rd As SqlDataReader = cmd.ExecuteReader()
        If (rd.Read()) Then
            GridView1.DataSource = dt
            GridView1.DataBind()
        Else
            GridView1.DataSource = ""
            GridView1.DataBind()
            Response.Write("<script>alert('No Records')</script>")
        End If

    End Sub
    Protected Sub BindDropDown()
        ' Name bind To Dropdownlist
        dbconn()
        Dim cmd As SqlCommand = New SqlCommand("select name from reg", con)
        Dim rd As SqlDataReader = cmd.ExecuteReader()
        While (rd.Read())
            DropDownList1.Items.Add(rd(0).ToString())
        End While

    End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgsHandles GridView1.SelectedIndexChanged
    End Sub
End Class























0 comments:

Post a Comment