Setting link dynamically

  • Thread starter Thread starter Morten Snedker
  • Start date Start date
M

Morten Snedker

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
 
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
 
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"

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.
 
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
 
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
 
On Wed, 3 Jan 2007 08:25:39 -0500, "Karl Seguin"

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top