Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2007, 08:00 PM
Authorized User
 
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to yukijocelyn
Default

Hi! Thanks! Okiez! I think I have to reaccess the database again.
Couldn't think of other methods to be able to not access the databasr again to get the email address.
Thank you so much for helping me out!

 
Old September 26th, 2007, 07:26 AM
Authorized User
 
Join Date: Dec 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi all,

There is no need to go back to Database.

Just split the string. Like this-

Dim str As String = "Remy|[email protected]"
Dim arr As Array
arr = str.Split("|")
MsgBox(UBound(arr))
MsgBox(arr(0))
MsgBox(arr(1))

By arr(0) u can get Name and by arr(1) u can get e-mail address.

Try & Enjoy.



 
Old September 26th, 2007, 09:58 PM
Authorized User
 
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to yukijocelyn
Default

Hi! Thanks for your suggestion! It saved me alot of time!
By th way, what does Ubound do?
Thank you!

 
Old September 27th, 2007, 03:37 AM
Authorized User
 
Join Date: Dec 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Ubound() function is return upper bound of array.

in this case arr holds two elements then Ubound() function returns 1.

Array is starting from 0 then arr(0) and arr(1) two are elements of array. if there are more elements in array then Ubound() function is used to get the no of elements in array.

Thanks.

 
Old September 27th, 2007, 04:01 AM
Authorized User
 
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I personally wouldn't split it. There are few problems with that approach:

First of all, if the name contains '|' character, your current solution won't work. Additional code is required for that. Of course, you can force the name to not contain any '|', but that's a whole different story.

Second, what if the item you selected from DropDownList gets deleted or updated in the database between the page loads? I'm not sure if DropDownList automatically handles this, but if it doesn't, you are going to end up, at some point, working with non-existent or out-of-date data. You can, of course, enforce the item can still be found in the database and hasn't changed. And for that, you need to access the database.

One database access doesn't really cost much (and you can even cache the data), and I personally think it's much better solution to retrieve the data you need from the database instead of splitting the contents of DropDownList item. It also makes expanding functionality in future a lot easier. For example, if you, at some point, need the phone number in addition to the email address, it would be just a matter of adding one additional column to the select clause. With your current solution, however, you would end up doing it all over again.

Unfortunately I don't have an example solution to give you, because I really haven't got my hands dirty yet with doing database stuff in .NET, but these are just some things you should consider.

 
Old September 27th, 2007, 04:52 AM
Authorized User
 
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to yukijocelyn
Default

Actually, I couldn't find the solution to access the database again. My 1st concern is because I combined the 2 data already. And this combined data is not going to appear as a new field, saved in my DB. So if I re-access it, I have to come up with SQL statement:
dim selectedvalue = DropDownList1.selectedvalue
SELECT * FROM table where email where name + "|" + email = selectedvalue

However, coming to think of it, i initially used the "AS Combined", I do not know to retrieve again. So I spilt it up.

Do you have other suggestions to overcome this problem?

Thank you!

 
Old September 27th, 2007, 06:34 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========
 
Old September 27th, 2007, 06:49 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Something else that I forgot to mention, Miko does make a good point about checking for the existance of the data: your UI would not automatically update itself if, for example, someone deleted X row from the table after you have bound your data to the dropdownlist.

By and large I do not provide DELETE functionality in my applications, in the classic sense anyway. Any time I have to give the user the ability to DELETE normally when they press X button to delete something all it does is hide the row of data from the UI by changing the value of a bit column.

So, alot depends on your business rules, if you are giving the users the ability to delete rows of data you should always check that the row exists before you begin working with it. However if your business rules simply "hide" data you can be fairly certain that the data is there.

hth.

================================================== =========
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
================================================== =========
 
Old September 28th, 2007, 05:13 AM
Authorized User
 
Join Date: Dec 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Miko2,

I have solutions for all your problems. But first u think what u want to help from us.

If you want to learn someting then be positive. If u have negative attitute all time u cant learn someting. You just tell your problem i will provide solution.

First of all you want to make seperate items that u added in combo prior without going to database then split is only solution.

And if you r worry about the changes made in database after your page load, in this case it is also possible to make changes after your work done. so don't worry about it.

If u are worry about no of items added in combo like empid,empname,email,phone then you have to use an array to store length of items.

Suppose length of these fields are
empid = 10
empname = 20
email = 20
phone = 10

Dim emparr as int()
emparr(0)=10
emparr(1)=20
emparr(2)=20
emparr(3)=10

At the time of seperating information form that combo u can use substr function.






 
Old September 28th, 2007, 06:12 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Whoop!! I have gone through all the posts, a lot of suggestions. Just one comment, reply given by parsons is wonderful and accurate (both conceptually & logically), so I guess the original poster should follow it.

good point 2 point explanation parsons.

Regards
Mike





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple views of single table jatatman ASP.NET 3.5 Basics 1 June 2nd, 2008 01:34 AM
converting Access 2000 views to Sql views matta Classic ASP Professional 1 January 26th, 2005 03:37 PM
Empty table for dropdownlist. macupryk BOOK: ASP.NET Website Programming Problem-Design-Solution 2 August 16th, 2004 06:33 AM
selected value from dropdownlist control netwizard_01 ASP.NET 1.0 and 1.1 Basics 3 January 20th, 2004 09:29 AM
DropDownList control bmains ASP.NET 1.0 and 1.1 Basics 4 November 21st, 2003 01:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.