Results 1 to 5 of 5

Thread: populating of a ddl

  1. #1

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    populating of a ddl

    want to populate a drop down list
    my code doesnt seem to work, is it the fill method is a problem??

    in my table i have just 2 columns with only one column name
    table name = tb_area
    columns = area_code, area_name

    i just want to display area name.

    Private ddlArea As New DropDownList
    Private daArea As New SqlDataAdapter

    .................

    daArea.SelectCommand = New SqlCommand("SELECT * from tb_area", oSQLConn)
    ddl_area.Fill(ddlArea)

    ddlArea.DataSource = daArea.Tables(0)
    ddlArea.DataBind()
    oSQLConn.Close()

  2. #2
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: populating of a ddl

    Try:

    Code:
    OleDbCommand myCmd = new OleDbCommand("SELECT * from tb_area", oSQLConn)
    
    OleDbDataReader myReader;
    
    oSQLConn.Open();
    
    myReader = myCmd.ExecuteReader();
    
    DDL.DataSource = myReader;
    DDL.DataTextField = "area_name";
    DDL.DataValueField = "area_name";
    DDL.DataBind();
    
    myReader.Close();
    oSQLConn.Close();

  3. #3

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populating of a ddl

    mine is in sql and i dont have a data reader declared , when i try to declare one i get the dreaded squiggle lines.

    Code:
     Private Sub fill_area()
    
            Dim sqlfillarea As New SqlCommand("SELECT * from tb_area", oSQLConn)
            'daArea.Fill()
    
            oSQLConn.Open()
            Me.ddlArea.DataSource = daArea
            Me.ddlArea.DataValueField = "area_name"
            Me.ddlArea.DataTextField = "area_name"
            Me.ddlArea.DataBind()
            oSQLConn.Close()
    Last edited by d2005; Sep 2nd, 2005 at 04:15 AM.

  4. #4
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: populating of a ddl

    How are you declaring your SqlDataReader object, can i see the code?

  5. #5

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populating of a ddl

    dont think i have a datareader, heres the whole heap,
    any help would be much appreciated

    VB Code:
    1. Imports System
    2. Imports System.data
    3. Imports System.Data.SqlClient
    4.  
    5.  
    6. Public Class WebForm1
    7.     Inherits System.Web.UI.Page
    8.  
    9. #Region " Web Form Designer Generated Code "
    10.  
    11.  
    12.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    13.         'CODEGEN: This method call is required by the Web Form Designer
    14.         'Do not modify it using the code editor.
    15.         InitializeComponent()
    16.     End Sub
    17.  
    18. #End Region
    19.  
    20.     ' declare the data set and data adapters
    21.     Private ddlArea As New DropDownList
    22.     Private daArea As New SqlDataAdapter
    23.  
    24.     Dim cmdselect As SqlCommand
    25.     Dim area_reader As SqlDataReader
    26.  
    27.  
    28.     'declare connection variable to database
    29.     Dim oSQLConn As SqlConnection = New SqlConnection
    30.  
    31.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    32.         'on page load connect to databse
    33.         user_reg()
    34.         ' fill_area()
    35.  
    36.  
    37.     End Sub
    38.  
    39.  
    40.  
    41.     Private Sub user_reg()
    42.         ' connect to the databse
    43.         oSQLConn.ConnectionString = "Data Source=(local);" & _
    44.        "Initial Catalog=TaT;" & _
    45.        "Integrated Security=SSPI"
    46.         oSQLConn.Open()
    47.  
    48.  
    49.         oSQLConn.Close()
    50.  
    51.  
    52.     End Sub
    53.     Private Sub fill_area()
    54.  
    55.         Dim sqlfillarea As New SqlCommand("SELECT * from tb_area", oSQLConn)
    56.         'ddlArea.fill(ddlArea)
    57.  
    58.  
    59.         oSQLConn.Open()
    60.         Me.ddlArea.DataSource = daArea
    61.         Me.ddlArea.DataValueField = "area_name"
    62.         Me.ddlArea.DataTextField = "area_name"
    63.         Me.ddlArea.DataBind()
    64.         oSQLConn.Close()
    65.  
    66.     End Sub
    67.  
    68.     Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
    69.         Dim regtime As New DateTime
    70.  
    71.         'use textboxes as entries
    72.         '    ,c_user_postcode,c_user_tel_alt,c_user_country, ' now() ','uk'
    73.         Dim sqlInsertuser As New SqlCommand("INSERT INTO tb_user(c_user_id,c_user_fname,c_user_sname,c_username_login,c_user_password,c_user_house,c_user_street,c_user_town,c_user_country,c_user_postcode,c_user_email,c_user_tel_alt,c_county,c_date_registered) VALUES('" & txt_mobile.Text & "','" & txt_fname.Text & "','" & txt_sname.Text & "','" & txt_username.Text & "','" & txt_password.Text & "','" & txt_house.Text & "','" & txt_street.Text & "','" & txt_town.Text & "','uk','" & txt_post.Text & "','" & txt_email.Text & "','" & txt_tel.Text & "','" & txt_county.Text & "',getDate());", oSQLConn)
    74.         sqlInsertuser.Connection.Open()
    75.         sqlInsertuser.ExecuteNonQuery()
    76.         oSQLConn.Close()
    77.  
    78.  
    79.  
    80.     End Sub

    the inserts are working ok but the ddl is not doin nothin, this is a test page for learning purposes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width