Pozdrav
Evo ga primer, doduse ne radi INSERT vec samo vezuje dropdownlist sa podacima iz baze i na selekt ispisuje ID. To bi trebalo da bude dovoljno. U pitanju je pubs baza iz MS SQL.
CodeBehind (WebForm1.aspx.vb)
Code:
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents titles As System.Web.UI.WebControls.DropDownList
Protected WithEvents title_id As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim cn As New SqlConnection(ConnectionString)
cn.Open()
Dim cmd As New SqlCommand("SELECT title_id, title FROM titles", cn)
Dim dr As SqlDataReader
dr = cmd.ExecuteReader
titles.DataSource = dr
titles.DataValueField = "title_id"
titles.DataTextField = "title"
titles.DataBind()
dr.Close()
cn.Close()
End If
End Sub
Private Sub titles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles titles.SelectedIndexChanged
title_id.Text = titles.SelectedItem.Value
End Sub
End Class
ASPX (WebForm1.aspx)
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication3.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="titles" runat="server" AutoPostBack="True"></asp:DropDownList><br><br>
<asp:Label id="title_id" runat="server"></asp:Label>
</form>
</body>
</HTML>