473,804 Members | 3,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Com mon.DbDataRecor d

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;U ser ID=sa;Password= password"
strSQL = "Select Title FROM [T-TitleList]"

objConnection = New SqlConnection(s trConnection)
objConnection.O pen()

objCommand = New SqlCommand(strS QL, objConnection)

DropDownList1.D ataSource =
objCommand.Exec uteReader(Comma ndBehavior.Clos eConnection)
DropDownList1.D ataBind()

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

DropDownList1.D ataSource =
objCommand.Exec uteReader(Comma ndBehavior.Clos eConnection)
DropDownList1.D ataTextfield = "Title"
DropDownList1.D ataValuefield = "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.D ataBind()
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*******@hotm ail.com> wrote in message
news:ea******** ******@TK2MSFTN GP14.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.Com mon.DbDataRecor d

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;U ser ID=sa;Password= password"
strSQL = "Select Title FROM [T-TitleList]"

objConnection = New SqlConnection(s trConnection)
objConnection.O pen()

objCommand = New SqlCommand(strS QL, objConnection)

DropDownList1.D ataSource =
objCommand.Exec uteReader(Comma ndBehavior.Clos eConnection)
DropDownList1.D ataBind()

objConnection.C lose()
Thanks,
Jim

Nov 19 '05 #2

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

DropDownList1.D ataSource =
objCommand.Exec uteReader(Comma ndBehavior.Clos eConnection)
DropDownList1.D ataTextfield = "Title"
DropDownList1.D ataValuefield = "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.D ataBind()
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__440B1D6 1'. 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.Sql Client

Partial Class insert1
Inherits System.Web.UI.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
lblFirstName.Te xt = "First Name"
lblLastName.Tex t = "Last Name"
lblEmployeeID.T ext = "Employee ID"
lblTitle.Text = "Employee Title"

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

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

objConnection = New SqlConnection(s trConnection)
objConnection.O pen()

objCommand = New SqlCommand(strS QL, objConnection)

ddlTitle.DataSo urce =
objCommand.Exec uteReader(Comma ndBehavior.Clos eConnection)
ddlTitle.DataTe xtField = "Title"
ddlTitle.DataBi nd()

objConnection.C lose()

End Sub

Protected Sub btnSubmit_Click (ByVal sender As Object, ByVal e As
System.EventArg s) 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;U ser
ID=sa;Password= password"
strSQL1 = "INSERT INTO [T-Master] (EmpID, FirstName, LastName)
VALUES ('" & _
txtEmployeeID.T ext & "','" & txtFirstName.Te xt & "','" &
_
txtLastName.Tex t & "')"

strSQL2 = "INSERT INTO [T-Title] (EmpID, Title) VALUES ('" & _
txtEmployeeID.T ext & "','" &
ddlTitle.Select edValue.ToStrin g & "')"
strSQL3 = "Select * From View1"
objConnection = New SqlConnection(s trConnection)
objConnection.O pen()

objCommand1 = New SqlCommand(strS QL1, objConnection)
objCommand2 = New SqlCommand(strS QL2, objConnection)
objCommand3 = New SqlCommand(strS QL3, objConnection)
objCommand1.Exe cuteNonQuery()
objCommand2.Exe cuteNonQuery()

dgList1.DataSou rce =
objCommand3.Exe cuteReader(Comm andBehavior.Clo seConnection)
dgList1.DataBin d()

objConnection.C lose()
End Sub
End Class

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

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

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

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

<!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>Untitl ed Page</title>
</head>
<body style="backgrou nd-color:Black;">
<form id="form1" runat="server">
<table border="1" bordercolor=whi te>
<tr>
<td valign=top style="width: 155px"><div>
<asp:Label ID="lblFirstNam e" runat="server" Font-Names="Monotype
Corsiva" Font-Size="X-Large"
Text="Label" ForeColor="Aqua "></asp:Label><br />
<asp:TextBox ID="txtFirstNam e" runat="server" BackColor="Silv er"
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="Silv er"
Font-Names="Arial Narrow" ForeColor="Navy "></asp:TextBox><br />
<br />
<asp:Label ID="lblEmployee ID" runat="server" Font-Names="Monotype
Corsiva" Font-Size="X-Large"
Text="Label" ForeColor="Aqua "></asp:Label><br />
<asp:TextBox ID="txtEmployee ID" runat="server" BackColor="Silv er"
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:DropDownLi st ID="ddlTitle" runat="server" Width="384px"
BackColor="Silv er" Font-Names="Arial Narrow" ForeColor="Navy ">
</asp:DropDownLis t><br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit Info"
Width="152px" BackColor="Blac k" BorderColor="Aq ua" BorderStyle="So lid"
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="Si lver" PagerStyle-ForeColor="Whit e"
HeaderStyle-BackColor="Navy " HeaderStyle-Font-Bold="True"
HeaderStyle-ForeColor="Whit e" ForeColor="Whit e" >
<PagerStyle BorderColor="Si lver" ForeColor="Whit e" />
<HeaderStyle BackColor="Navy " Font-Bold="True" ForeColor="Whit e" />
</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
19191
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
54
6586
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 FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
4
2732
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 namespace boost; struct C {
24
2969
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 reading several old posts/threads on the subject, and they never end with a conclusion (people keep correcting each other and disagreeing).
1
1604
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 do have integer selectionid "targetid" in my control and I'm trying to bind the (int) selectionid "sourceid" from my custom data source. How can it be done? I've tried the following with no luck: control.DataBindings.Add( "targetId",...
3
2060
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 table should be created or not. In order to do this, I need to have full control over the conditional logic for how items get displayed within a repeater element which I'm not seeing as possible. How can I cursor through a data set, apply...
6
6973
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 object, which has 1+ Shipment objects, which each have 1+ LineItem objects. Pretty standard. I'm looking to print a report that prints out the header for the shopping cart, and then sections for each shipment, and so on. I am floundering on how...
167
8376
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 object should run in its own thread, it should implement this mix-in class. Does this sound like plausible design decision? I'm surprised that C++ doesn't have such functionality, say in its STL. This absence of a thread/object relationship in...
3
3345
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 boost::bind), but the compiler complains, because of a type mismatch in an assignment which I think should be legal:
0
9587
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7623
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.