Monday, March 26, 2012

Newbie Code Question

Would somebody mind taking a look at some code for me, It's a beginners attempt at a n-tier architecture using .NET 2.0 and MSDE, and it mostly works but has one problem - when I use the webform to create a new record, it creates two records in the database! I've rewritten it once already, and just cannot see what the problem is. I guess I'm somehow calling the save method twice by doing something fundamentally wrong with objects, but I cannot see what!

I would really appreciate any feedback - thanks in advance.


Webform code:

PrivateSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMyBase.Load
Dim myOrganisationAs CRM.Organisation
Dim strOrganisationIdAsString

strOrganisationId = Request.QueryString("id")

IfNot Page.IsPostBackThen
myOrganisation =New CRM.Organisation

IfNot strOrganisationId =""Then
'read data and push to web form
myOrganisation.GetOrganisastionById(CLng(strOrganisationId))

'organisation_id.Text = CStr(myOrganisation.Id)
title.Text = myOrganisation.Name
OrganisationName.Text = myOrganisation.Name
OrganisationURL.Text = myOrganisation.URL
EndIf

'save in ViewState for postbacks
ViewState("currentOrganisation") = myOrganisation
EndIf

EndSub


ProtectedSub SaveButton_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Button1.Click
Dim myOrganisationAs CRM.Organisation
Dim intResultAsInteger

myOrganisation =CType(ViewState("currentOrganisation"), CRM.Organisation)
myOrganisation.Name = OrganisationName.Text
myOrganisation.URL = OrganisationURL.Text

intResult = myOrganisation.Store()
EndSub

BLL code:

<Serializable()>PublicClass Organisation

Private organisationIdAsLong
Private organisationNameAsString

PublicSub GetOrganisationById(ByVal requestedIdAsLong)
Dim databaseOutputAs DataSet

databaseOutput = CRM.Data.Organisation.GetOrganisationById(requestedId)
organisationId =CLng(databaseOutput.Tables(0).Rows(0)("organisation_id").ToString)
organisationName = databaseOutput.Tables(0).Rows(0)("organisation_name").ToString
EndSub
PublicFunction Save()AsInteger
Return CRM.Data.Organisation.SaveChanges(Me)
EndFunction
End Class

Data Layer Code:

PublicClass DataLayer

PublicClass Organisation

PublicSharedFunction GetOrganisationById(ByVal requestedIdAsLong)As DataSet
Dim params(0)As SqlParameter
params(0) =New SqlParameter("@dotnet.itags.org.organisation_id", requestedId)
Return DataAccessHelper.ExecuteDataSet("select * from organisations where organisation_id=@dotnet.itags.org.organisation_id", params)
EndFunction

PublicSharedFunction Update(ByVal sourceOrganisationAs CRM.Organisation)AsInteger
Dim params(2)As SqlParameter
Dim sqlCodeAsString

sqlCode ="update organisations set organisation_name=@dotnet.itags.org.organisation_name, url = @dotnet.itags.org.url where organisation_id=@dotnet.itags.org.organisation_id"
params(0) =New SqlParameter("@dotnet.itags.org.organisation_id", sourceOrganisation.Id)
params(1) =New SqlParameter("@dotnet.itags.org.organisation_name", sourceOrganisation.Name)
params(2) =New SqlParameter("@dotnet.itags.org.url", sourceOrganisation.Name)
Return DataAccessHelper.ExecuteNonQuery(sqlCode, params)

EndFunction

PublicSharedFunction Insert(ByVal sourceOrganisationAs CRM.Organisation)AsInteger
Dim params(0)As SqlParameter
Dim sqlCodeAsString
sqlCode ="insert into organisations (organisation_name) values (@dotnet.itags.org.organisation_name)"
params(0) =New SqlParameter("@dotnet.itags.org.organisation_name", sourceOrganisation.Name)
Return DataAccessHelper.ExecuteNonQuery(sqlCode, params)
EndFunction

EndClass

EndClass

I think you need to post a bit more of your code. To start with, the Click handler calls a method called Store(), which is not shown in the BLL's Organisation class. Talking of which, the Save() method calls a method SaveChanges in the Data Organisation class, which is also not shown.
Without these two methods, it's a little hard to see what's going on.
One other thing to watch is that you haven't got an OnClick= in the .aspx markup as well as the Handles Button1.Click on the VB code. This would result in the the click event handler being called twice.
Dave, thanks for taking the time to have a look at the code. I actually managed to figure out what the problem was lastnight (uk time!).
You must be psychic, it was indeed because I had an onclick attribute on the button, and the same sub is the handler for the button click. Doh!!!Embarrassed [:$] Possibly a common mistake for beginners??!!
Thanks again.

0 comments:

Post a Comment