Wednesday, 16 April 2014

Get Data to DropDownList from DB using Class file in VB.NET

Ans:-

Default2.aspx:-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get State Data Page</title>
</head>
<body>
    <form id="form1" runat="server">
     <center>
     <div>
    <table>
    <tr>
    <td>
    <asp:HyperLink ID="Hiperlink" runat ="server" NavigateUrl ="~/Default.aspx" Target ="_blank" Text="Go To Default.aspx" ></asp:HyperLink>
    </td>
    </tr>
    <tr>
    <td align="left">
   <asp:Label ID="lblState" runat="server" Text="State Code" Width="115px"></asp:Label>
                                    </td>
                                    <td align="left">
                                     <asp:DropDownList ID="ddlState" runat="server" Width="100px" TabIndex="3">
                                    </asp:DropDownList>
                                    </td>
                                    <td>
                                        <asp:Button ID="Button1" runat="server"
                                            Text="Button" />
                                    </td>
    </tr>
    </table>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </center>
    </form>
</body>
</html>

Default2.aspx.cs:-

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls

Partial Class Default2
    Inherits System.Web.UI.Page
    Dim obj As New Class1
    Dim sqlCon As SqlConnection

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        sqlCon = New SqlConnection(obj.GetConnection())
        sqlCon.Open()
        If Not IsPostBack Then
            ddlState.Items.Add("")
            obj.AddCombo(ddlState, "select CAST(sid as varchar)+'-'+sname as states from state", sqlCon)
            ddlState.Items.Insert(1, "All")
            ddlState.Visible = True

        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = ddlState.SelectedValue
    End Sub
End Class

Class1.cs:-

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient

Public Class Class1
    Dim sqlcmd As SqlCommand
    Dim sqldr As SqlDataReader
    Public Function GetConnection() As String
        Dim conStr As String = "Server=.;database=SubsDB;UID=sa;Password=123;Pooling=true;Min Pool Size=2;Max Pool Size=1000"
        Return conStr
    End Function
    Public Sub AddCombo(ByVal CntName As DropDownList, ByVal SqlStr As String, ByVal sCon As SqlConnection)
        'Which will fill the DropdownList Based on Query
        Try
            CntName.Items.Clear()
            CntName.Items.Add("")
            sqlcmd = New SqlCommand(SqlStr, sCon)
            sqldr = sqlcmd.ExecuteReader()
            While sqldr.Read
                CntName.Items.Add(sqldr(0))
            End While
            sqldr.Close() : sqlcmd.Dispose()
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
End Class

Create Table and Stored Procedure:-

create table state(sid int,sname varchar(30))

No comments:

Post a Comment