Saturday, March 24, 2012

Newbie has coding problem (2 Qs)

Making a volunteer registration form using sproc and C# - integrated in the portal starter kit.

Code follows, then compiler error. Q. What is the better way to test for a null record? I know my HasRec variable is bad coding, but unsure how to use Isnull in C# against a SQL record.


<%@dotnet.itags.org. Page Language="c#" %>
<%@dotnet.itags.org. Register TagPrefix="portal" TagName="Banner" src="http://pics.10026.com/?src=~/DesktopPortalBanner.ascx" %>
<script runat="server"
int HasRec = 0;

void Page_Load(int userID)
{

// Check to see if data is in this userID rec and display if so
// create instance {new?}

SqlDataReader dr = profile.GetVolunteerProfilePageOne(UserID);

dr.read()

SalutationDropDownList.Text = (string) dr["Salutation"];
FirstNameTextBox.Text = (string) dr["Firstname"];
MiddlenameTextBox.Text = (string) dr["Middlename"];
LastnameTextBox.Text = (string) dr["Lastname"];
SuffixTextBox.Text = (string) dr["Suffix"];
AddressTextBox.Text = (string) dr["Address"];
AptNumberTextBox.Text = (string) dr["AptNumber"];
ZIPTextBox.Text = (int) dr["Zip"];
JurisdictionDropDownList.Text = (string) dr ["Jurisdiction"];
StateTextBox.Text = (string) dr["State"];
POBoxTextBox.Text = (string) dr["POBox"];
DOBTextBox.Text = (string) dr["DOB"];
SocialSecurityTextBox.Text = (string) dr["SocialSecurity"];

if Lastname != null {

HasRec = 1;
}

// Close datareader
dr.Close();

}

}

void FirstRegPageButton_Click(object sender, EventArgs e) {

if Hasrec = 0 {

// Add the profile within the profile table
profile.CreateVolunteerProfilePageOne ( UserID, SalutationDropDownList.Text, FirstNameTextBox.Text, MiddleNameTextBox.Text, LastNameTextBox.Text, SuffixTextBox.Text, AddressTextBox.Text, AptNumberTextBox.Text, ZIPTextBox.Int, JurisdictionDropdownList.Selection, StateTextBox.Text, POBoxTextBox.Text, DOBTextBox.Text, SocialSecurityTextBox.Text);
}
else {

// Update the contact within the contacts table
profile.UpdateVolunteerProfilePageOne ( UserID, SalutationDropDownList.Text, FirstNameTextBox.Text, MiddleNameTextBox.Text, LastNameTextBox.Text, SuffixTextBox.Text, AddressTextBox.Text, AptNumberTextBox.Text, ZIPTextBox.Int, JurisdictionDropdownList.Selection, StateTextBox.Text, POBoxTextBox.Text, DOBTextBox.Text, SocialSecurityTextBox.Text);
}
}

void Cancel_Click(object sender, EventArgs e) {

}

</script

Compiler Error:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0116: A namespace does not directly contain members such as fields or methods

Source Error:

Line 48: }
Line 49:
Line 50: void FirstRegPageButton_Click(object sender, EventArgs e) {
Line 51:
Line 52:

Source File: C:\Portal\PortalCSSDK\DesktopModules\VolRegistration.aspx Line: 50You are getting the compilation error because you have an extra closing bracket "}" at line 48 - remove it and it should work.

Hope that helps
Kashif
You can test for a null record with a SqlDataReader by the Read method's return value.

bool noRecords = dr.Read()

If noRecords is false, then there are no more records at the current position in the reader.

The way to test for a null field in a record is to test against DBNull.Value.

if (dr["LastName"] == DBNull.Value)
//field is null

0 comments:

Post a Comment