473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

What Objects To Use & How To Properly Bind

Being new, i experiment a lot and usually get results I'm not hoping for. My
current problem is reading from a SQL 2000 table and populating an ASP
dropdownlist with a column from the table. The only thing displayed in the
DropDownList is:

System.Data.Common.DbDataRecord

So, under the Page_Load Event, here's my code:

Dim strSQL, strConnection As String
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand

strConnection = "server=sqldev;database=test;User ID=sa;Password=password"
strSQL = "Select Title FROM [T-TitleList]"

objConnection = New SqlConnection(strConnection)
objConnection.Open()

objCommand = New SqlCommand(strSQL, objConnection)

DropDownList1.DataSource =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
DropDownList1.DataBind()

objConnection.Close()
Thanks,
Jim
Nov 19 '05 #1
2 1683
Specify the DataTextField and DataValueField of the dropdown...

DropDownList1.DataSource =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
DropDownList1.DataTextfield = "Title"
DropDownList1.DataValuefield = "TitleId" 'that won't work beceause you
don't have a TitleId, so you could just reuse Title..but that might not be
unique...
DropDownList1.DataBind()
anyways, having Data access directly in your page_load is a sign that you
don't have a business or data layer ;) I know ur just playing around ...

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Jim in Arizona" <ti*******@hotmail.com> wrote in message
news:ea**************@TK2MSFTNGP14.phx.gbl...
Being new, i experiment a lot and usually get results I'm not hoping for.
My current problem is reading from a SQL 2000 table and populating an ASP
dropdownlist with a column from the table. The only thing displayed in the
DropDownList is:

System.Data.Common.DbDataRecord

So, under the Page_Load Event, here's my code:

Dim strSQL, strConnection As String
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand

strConnection = "server=sqldev;database=test;User ID=sa;Password=password"
strSQL = "Select Title FROM [T-TitleList]"

objConnection = New SqlConnection(strConnection)
objConnection.Open()

objCommand = New SqlCommand(strSQL, objConnection)

DropDownList1.DataSource =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
DropDownList1.DataBind()

objConnection.Close()
Thanks,
Jim

Nov 19 '05 #2

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uW*************@TK2MSFTNGP15.phx.gbl...
Specify the DataTextField and DataValueField of the dropdown...

DropDownList1.DataSource =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
DropDownList1.DataTextfield = "Title"
DropDownList1.DataValuefield = "TitleId" 'that won't work beceause you
don't have a TitleId, so you could just reuse Title..but that might not be
unique...
DropDownList1.DataBind()
anyways, having Data access directly in your page_load is a sign that you
don't have a business or data layer ;) I know ur just playing around ...

Karl


That worked. I had never tried binding data to a dropdownlist before (as you
may have figured out).

I do have a TitleID field within the database that is the PK and an Identity
type so auto increments for me. I didn't see any reason to who the PK on the
webpage anywhere though.

I'm not sure what you mean by Business or Data layer.

My project was to try to add data into related tables successfully. All my
work in the past has been adding data into one table, which has been fairly
easy to do. It's adding data to two related tables at the same time that I
hadn't tried and wasn't sure how to successfully do it. I am running into a
duplicate value problem now.

My Tables:

[T-Master]
EmpID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50)

[T-Title]
TitleID INT PRIMARY KEY IDENTITY NOT NULL,
EmpID INT,
CONSTRAINT EmpID_FK FOREIGN KEY(EmpID)
REFERENCES [T-Master](EmpID)

[T-TitleList]
TitleID INT PRIMARY KEY IDENTITY NOT NULL,
Title VARCHAR(200)

The TitleList table is just a table to popluate the DropDownList.

The relationship between T-Master and T-Title is a one to many so an
Employee can have more than one Title.
I understand that I can't have a duplicate employee within the T-Master
table with the same EmpID.

In my code (show below), I can do the insert into both tables just fine and
it appears to work successfully. But, if I try to submit again with the same
employee but different Title, I get the Error:

Violation of PRIMARY KEY constraint 'PK__T-Master__440B1D61'. Cannot insert
duplicate key in object 'T-Master'.
The statement has been terminated.

I do understand what's happening here. So, how would I modify my code so
that I could add another Title to the Employee's title within the T-Title
table without violating the key constraint in T-Master?

Here's my Code:
====================================

Imports System.Data
Imports System.Data.SqlClient

Partial Class insert1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
lblFirstName.Text = "First Name"
lblLastName.Text = "Last Name"
lblEmployeeID.Text = "Employee ID"
lblTitle.Text = "Employee Title"

Dim strSQL, strConnection As String
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand

strConnection = "server=sqldev;database=test;User
ID=sa;Password=password"
strSQL = "Select Title FROM [T-TitleList]"

objConnection = New SqlConnection(strConnection)
objConnection.Open()

objCommand = New SqlCommand(strSQL, objConnection)

ddlTitle.DataSource =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
ddlTitle.DataTextField = "Title"
ddlTitle.DataBind()

objConnection.Close()

End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click
Dim strConnection, strSQL1, strSQL2, strSQL3 As String
Dim objConnection As SqlConnection
Dim objCommand1, objCommand2, objCommand3 As SqlCommand

strConnection = "server=sqldev;database=test;User
ID=sa;Password=password"
strSQL1 = "INSERT INTO [T-Master] (EmpID, FirstName, LastName)
VALUES ('" & _
txtEmployeeID.Text & "','" & txtFirstName.Text & "','" &
_
txtLastName.Text & "')"

strSQL2 = "INSERT INTO [T-Title] (EmpID, Title) VALUES ('" & _
txtEmployeeID.Text & "','" &
ddlTitle.SelectedValue.ToString & "')"
strSQL3 = "Select * From View1"
objConnection = New SqlConnection(strConnection)
objConnection.Open()

objCommand1 = New SqlCommand(strSQL1, objConnection)
objCommand2 = New SqlCommand(strSQL2, objConnection)
objCommand3 = New SqlCommand(strSQL3, objConnection)
objCommand1.ExecuteNonQuery()
objCommand2.ExecuteNonQuery()

dgList1.DataSource =
objCommand3.ExecuteReader(CommandBehavior.CloseCon nection)
dgList1.DataBind()

objConnection.Close()
End Sub
End Class

====================================

And here's the web form, if it matters:

====================================

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body style="background-color:Black;">
<form id="form1" runat="server">
<table border="1" bordercolor=white>
<tr>
<td valign=top style="width: 155px"><div>
<asp:Label ID="lblFirstName" runat="server" Font-Names="Monotype
Corsiva" Font-Size="X-Large"
Text="Label" ForeColor="Aqua"></asp:Label><br />
<asp:TextBox ID="txtFirstName" runat="server" BackColor="Silver"
Font-Names="Arial Narrow" ForeColor="Navy"></asp:TextBox><br />
<br />
<asp:Label ID="lblLastName" runat="server" Font-Names="Monotype
Corsiva" Font-Size="X-Large"
Text="Label" ForeColor="Aqua"></asp:Label><br />
<asp:TextBox ID="txtLastName" runat="server" BackColor="Silver"
Font-Names="Arial Narrow" ForeColor="Navy"></asp:TextBox><br />
<br />
<asp:Label ID="lblEmployeeID" runat="server" Font-Names="Monotype
Corsiva" Font-Size="X-Large"
Text="Label" ForeColor="Aqua"></asp:Label><br />
<asp:TextBox ID="txtEmployeeID" runat="server" BackColor="Silver"
Font-Names="Arial Narrow" ForeColor="Navy"></asp:TextBox><br />
<br />
<asp:Label ID="lblTitle" runat="server" Font-Names="Monotype
Corsiva" Font-Size="X-Large"
Text="Label" ForeColor="Aqua"></asp:Label><br />
<asp:DropDownList ID="ddlTitle" runat="server" Width="384px"
BackColor="Silver" Font-Names="Arial Narrow" ForeColor="Navy">
</asp:DropDownList><br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit Info"
Width="152px" BackColor="Black" BorderColor="Aqua" BorderStyle="Solid"
Font-Bold="True" Font-Names="Monotype Corsiva" Font-Size="Large"
ForeColor="Lime" />
</div></td>
<td valign=top><asp:DataGrid ID="dgList1" runat=server
PagerStyle-BorderColor="Silver" PagerStyle-ForeColor="White"
HeaderStyle-BackColor="Navy" HeaderStyle-Font-Bold="True"
HeaderStyle-ForeColor="White" ForeColor="White" >
<PagerStyle BorderColor="Silver" ForeColor="White" />
<HeaderStyle BackColor="Navy" Font-Bold="True" ForeColor="White" />
</asp:DataGrid></td>
</tr>
</table>

</form>
</body>
</html>

====================================

I am using Visual Studio 2005 Beta 2 with .NET Framework 2 Beta 2.

Thanks,
Jim



Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
4
by: Arturo Cuebas | last post by:
The program below contains a compile error. Following the program you will find the typical fix then my idea for a library that facilitates a more elegant fix. #include <boost\bind.hpp> using...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
1
by: Antero | last post by:
I'm desperate! :-/. I'm desperately trying to bind to objects together to share same data and to display it in a windows forms. I'm trying to bind an custom object to custom control property. I...
3
by: Ben Becker | last post by:
I am trying to build a custom crosstab type of grid where I take some items in a data grid and based on the content of the current item compared to the previous item, determine if a new row in a...
6
by: Susan H | last post by:
I have Visual Studio 2005, trying to properly bind to a nested object tree. I can bind to the top level object just fine, but can't seem to "hook" my nested object in. I have a ShoppingCart...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
3
by: Giovanni Gherdovich | last post by:
Hello, in the following code I have a pointer (to function), say p, of type double (*)(double, double, void*) and I try to fix the second argument of the function *p to a given value (using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.