O.o Ok I have been reading through this at some length and I am curious as to why you have your database table setup with just 2 varchar columns? The thing is you are making this much more difficult then it needs to be consider this Table layout
--Adding the Primary Key constraint is optional
CREATE TABLE Foo (
pk INT IDENTITY(1,1),
Name VARCHAR(255),
Email VARCHAR(255),
CONSTRAINT ID_PK PRIMARY KEY (pk)
)
Now, when data is inserted into this table pk will increment by 1 so if "Remy|
[email protected]" was inseted into the table, it would look like this
pk Name Email
1 Remy
[email protected]
2 Record2
[email protected]
3 Record3
[email protected]
Ok so now I might write a stored procedure such as:
CREATE Procedure fooProcedure
AS
SELECT
pk,
[Name] + '|' + [Email] as NameAndEmail
From Foo
The resultset that is returned would look like this:
pk NameAndEmail
1 Remy|
[email protected]
2 Record2|
[email protected]
3 Record3|
[email protected]
And that result set is what I would bind to my dropdown:
Code:
drp.DataValueField = "pk";
drp.DataTextField = "NameAndEmail";
drp.DataSource = SqlHelper.ExecuteDataSet(<connectionstring>,
CommandType.StoredProcedure,
"FooProcedure").Tables[0];
drp.DataBind();
So now you have the correct output that you want: Name|Email and on the value side of things you have a unique value, the pk, that is associated with each record so when you want to retrieve the data you might create a stored procedure like this:
CREATE Procedure GetFoo
(
@pk int
)
AS
SELECT * from Foo where pk = @pk
and then in code you might do something like this:
Code:
DataTable dt = SqlHelper.ExecuteDataSet(<connectionstring>,
CommandType.StoredProcedure,
"GetFoo",
new SqlParameter("@pk", Convert.ToInt32(drp.SelectedItem.Value))).Tables[0];
The variable DT will contain all of the row data from the table that is associated with the SelectedValue (pk) of the drop down.
hth.
(BTW, SqlHelper is a class that exists inside of the Microsoft.ApplicationBlocks.Data namespace that is NOT included with the .NET framework. You can read more about it here:
http://aspnet.4guysfromrolla.com/art...1.aspx IMHO it simplifies database work since the class does all the heavy lifting for you, all you have to do is make a call to it.)
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========