Saturday, March 31, 2012

Setting link dynamically

My code works as intended. I'm just curious if I am approaching it the
right way.

Images have a link upon them. These links can be changed, stored and
retrieved by a stored procedure. I use FindControl to set the
properties of each image. Is that the proper way to do it?

Regards /Snedker

Dim sqlCon As New SqlConnection, reader As SqlDataReader
sqlCon.ConnectionString = conString

Dim h As HyperLink

Try
Dim cmd As New SqlCommand
cmd.CommandText = "spSetAdLinks"
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Connection = sqlCon

sqlCon.Open()

reader = cmd.ExecuteReader

If Not reader.HasRows Then
Response.Write("No records")
Else
While reader.Read
h = FindControl(reader(1).ToString)
h.NavigateUrl = reader(2).ToString
h.Text = reader(3).ToString
h.Target = reader(4).ToString
End While
End IfYa..it's a fine way to do it.

Your code is a little rough though. The worst is that you don't have Option
Strict On, it's convinient in some cases, but will quickly become way more
trouble. Turn it on and fix the errors it reports.

Your data access layer and presentation layers are glued..which is fine for
a small project.

Your objects aren't being disposed of (although I see a try, I figure you
just left out the finally for clarities sake..).

Cheers,
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Morten Snedker" <fido@.alotofsites.comwrote in message
news:k7blp2l0cbolovhakg11uac4bhk1o6ee3r@.4ax.com...

Quote:

Originally Posted by

My code works as intended. I'm just curious if I am approaching it the
right way.
>
Images have a link upon them. These links can be changed, stored and
retrieved by a stored procedure. I use FindControl to set the
properties of each image. Is that the proper way to do it?
>
>
Regards /Snedker
>
>
>
Dim sqlCon As New SqlConnection, reader As SqlDataReader
sqlCon.ConnectionString = conString
>
Dim h As HyperLink
>
Try
Dim cmd As New SqlCommand
cmd.CommandText = "spSetAdLinks"
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Connection = sqlCon
>
sqlCon.Open()
>
reader = cmd.ExecuteReader
>
If Not reader.HasRows Then
Response.Write("No records")
Else
While reader.Read
h = FindControl(reader(1).ToString)
h.NavigateUrl = reader(2).ToString
h.Text = reader(3).ToString
h.Target = reader(4).ToString
End While
End If


Auch, you're evil! :-)

I suppose you by rough refer to something like

h = FindControl(reader(1).ToString)

, which I guess you knew would produce an error due to the implicit
type conversion, when having Option Strict set. Though, I just can't
figure out how to do the casting that makes i work..? And I really
tried! :-)

Regards /Snedker

On Tue, 2 Jan 2007 14:49:00 -0500, "Karl Seguin"
<karlremove@.removeopenmymindremovemetoo.andmenetwro te:

Quote:

Originally Posted by

>Ya..it's a fine way to do it.
>
>Your code is a little rough though. The worst is that you don't have Option
>Strict On, it's convinient in some cases, but will quickly become way more
>trouble. Turn it on and fix the errors it reports.
>
>Your data access layer and presentation layers are glued..which is fine for
>a small project.
>
>Your objects aren't being disposed of (although I see a try, I figure you
>just left out the finally for clarities sake..).
>
>Cheers,
>Karl


If all o fthe images are in the same container like a panel, you could
do panel.FindControl instead of Me.FindControl and then it'll be
searching a smaller collection.

Also you should handle what happens if FindControl returns Nothing.

HTH,

Sam

------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Tue, 02 Jan 2007 20:11:08 +0100, Morten Snedker
<fido@.alotofsites.comwrote:

Quote:

Originally Posted by

>My code works as intended. I'm just curious if I am approaching it the
>right way.
>
>Images have a link upon them. These links can be changed, stored and
>retrieved by a stored procedure. I use FindControl to set the
>properties of each image. Is that the proper way to do it?
>
>
>Regards /Snedker
>
>
>
>Dim sqlCon As New SqlConnection, reader As SqlDataReader
sqlCon.ConnectionString = conString
>
Dim h As HyperLink
>
Try
Dim cmd As New SqlCommand
cmd.CommandText = "spSetAdLinks"
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Connection = sqlCon
>
sqlCon.Open()
>
reader = cmd.ExecuteReader
>
If Not reader.HasRows Then
Response.Write("No records")
Else
While reader.Read
h = FindControl(reader(1).ToString)
h.NavigateUrl = reader(2).ToString
h.Text = reader(3).ToString
h.Target = reader(4).ToString
End While
End If


On Tue, 2 Jan 2007 14:49:00 -0500, "Karl Seguin"
<karlremove@.removeopenmymindremovemetoo.andmenetwro te:

As where a bigger project would be better approached how?

I'm fairly new to aspnet (and websites in general), so please be as
specific as possible as I'm sucking up all knowledge I stumble upon.

Quote:

Originally Posted by

>Your data access layer and presentation layers are glued..which is fine for
>a small project.


Sam's comments were very good also.

As for casting..

dim control as Control = FindControl(reader(1).ToString())
if (control is nothing) then
'throw an exception maybe? depends whether this is exceptional or not
else if (control is HtmlImage)
dim h as HtmlImage = ctype(control, HtmlImage)
h.NavigateUrl = ...
else if (control is TextBox)
dim t as TextBox = ctype(control, TextBox)
t.Text = reader(1).ToString()
end if

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Morten Snedker" <fido@.alotofsites.comwrote in message
news:veklp2l99u9u4pvm635cs0fq7f67eb3o65@.4ax.com...

Quote:

Originally Posted by

Auch, you're evil! :-)
>
I suppose you by rough refer to something like
>
h = FindControl(reader(1).ToString)
>
, which I guess you knew would produce an error due to the implicit
type conversion, when having Option Strict set. Though, I just can't
figure out how to do the casting that makes i work..? And I really
tried! :-)
>
>
Regards /Snedker
>
>
On Tue, 2 Jan 2007 14:49:00 -0500, "Karl Seguin"
<karlremove@.removeopenmymindremovemetoo.andmenetwro te:
>

Quote:

Originally Posted by

>>Ya..it's a fine way to do it.
>>
>>Your code is a little rough though. The worst is that you don't have
>>Option
>>Strict On, it's convinient in some cases, but will quickly become way more
>>trouble. Turn it on and fix the errors it reports.
>>
>>Your data access layer and presentation layers are glued..which is fine
>>for
>>a small project.
>>
>>Your objects aren't being disposed of (although I see a try, I figure you
>>just left out the finally for clarities sake..).
>>
>>Cheers,
>>Karl


>


At the very least, it's generally a good idea to have your dataaccess layer
sit in it's own class which your codebehind files can leverage.

something like:

Button_Click()
dim dal as new DataAccess()
dal.AddUser(User.Text, Password.Text)
end sub

or something..

For larger projects, you typically want a full domain layer...so you do
something like:

dim user as User = User.CreateNewUser();
user.FirstName = FirstName.Text;
user.LastName = LastName.Text;
user.Save();

I left out the validation you'd normally have..

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Morten Snedker" <fido@.alotofsites.comwrote in message
news:nfllp25mq6r3d354qev9tr5c06p868gd12@.4ax.com...

Quote:

Originally Posted by

On Tue, 2 Jan 2007 14:49:00 -0500, "Karl Seguin"
<karlremove@.removeopenmymindremovemetoo.andmenetwro te:
>
As where a bigger project would be better approached how?
>
I'm fairly new to aspnet (and websites in general), so please be as
specific as possible as I'm sucking up all knowledge I stumble upon.
>

Quote:

Originally Posted by

>>Your data access layer and presentation layers are glued..which is fine
>>for
>>a small project.


>


On Wed, 3 Jan 2007 08:25:39 -0500, "Karl Seguin"
<karlremove@.removeopenmymindremovemetoo.andmenetwro te:

It was the CType thing I'd forgotten all about. Funny how it slips
when you've been away from it for only a short while.

Thanks to both you and Sam for your fine resonse.

Regards /Morten

0 comments:

Post a Comment