Solved

Populate ASP Controls w/ Stored Procedure Return Results Failure

Posted on 2011-03-04
5
285 Views
Last Modified: 2013-11-11
I'm trying to populate controls on a .aspx page with result s from a stored procedure. It's not that it doesn't work it's not populating the way that it should be.

CODE:
protected void OrderNmSearchButton_Click(object sender, EventArgs e)
        {
            RMADataClassDataContext rmaOrderNum = new RMADataClassDataContext();

            var q = rmaOrderNum.sp_Incident_selectDetailsbyOrderNum(OrderNumtxtBox.Text);

            foreach (var i in q)
            {
                incidenttextbox.Text = i.IncidentID.ToString();
                ordertxtbox.Text = i.OrderID.ToString();
                emailtxtbox.Text = i.Email.ToString();
                nametxtbox.Text = i.Name.ToString();
                //Might Not Work
                ReasonDropDownList1.SelectedValue = i.IncidentReasonID.ToString();
                ResolutionDropDownList2.SelectedValue = i.IncidentResolutionID.ToString();
                ShipperDropDownList3.SelectedValue = i.ShipperID.ToString();
                CreatorDropDownList4.SelectedValue = i.IncidentCreatedBy.ToString();
                //
               
                // This Date Works
                origdatetxtbox.Text = i.OriginalShipDate.ToString();


               //These Dates Do Not Work
                datecreatedtxtbox.Text = i.IncidentCreatedDate.ToString();
                returntagdatetxtbox.Text = i.DateRetTagGenerated.ToString();
                ebaycreditdatetxtbox.Text = i.eBayCreditFileDate.ToString();
                increturndatetxtbox.Text = i.IncidentReturnDate.ToString();
               
               
                //dropdownboxes values selected
               
                //These dropdownlists work
                ReturnTagDropDownList5.SelectedValue = ReturnTagDropDownList5.Items.FindByText(i.ReturnTagRequired.ToString()).Value;
                ebaycreditDropDownList7.SelectedValue = ebaycreditDropDownList7.Items.FindByText(i.eBayCredit.ToString()).Value;
                RMADropDownList8.SelectedValue = RMADropDownList8.Items.FindByText(i.RMARequested.ToString()).Value;
                IncClosedDropDownList9.SelectedValue = IncClosedDropDownList9.Items.FindByText(i.IncidentResolved.ToString()).Value;

               


               //These Do not work
                //IncReceivedDropDownList10.SelectedValue = IncReceivedDropDownList10.Items.FindByText(i.IncidentReceived.ToString().Trim()).Value;
                //RefundDropDownList6.SelectedValue = RefundDropDownList6.Items.FindByText(i.RefundRequired.ToString()).Value;



                //Notes

                if (i.Notes.ToString() == null)
                {
                    notestxtbox.Text = "";
                }
                else
                {
                    notestxtbox.Text = i.Notes.ToString();
                }

                //Refund Date

                if (i.DateRefundIssued.ToString() == null)
                {
                    refunddatetxtbox.Text = "";
                }
                else
                {
                    refunddatetxtbox.Text = i.DateRefundIssued.ToString();
                }
                               
                //Refund Amount

                if (i.RefundAmount.ToString() == null)
                {
                    refamounttxtbox.Text = "";
                }
                else
                {
                    refamounttxtbox.Text = i.RefundAmount.ToString();
                }
               
                //Inventory Item Name

                if (i.InventoryItem.ToString() == null)
                {
                    itemnametxtbox.Text = "";
                }
                else
                {
                    itemnametxtbox.Text = i.InventoryItem.ToString();
                }
               
                //Item quantity

                if (i.InventoryItem.ToString() == null)
                {
                    itemqtytxtbox.Text = "";
                }
                else
                {
                    itemqtytxtbox.Text = i.InventoryQuantity.ToString();
                }
               
                //Lost Value

                if (i.LostValue.ToString() == null)
                {
                    lostvaletxtbox.Text ="";
                }
                else
                {
                    lostvaletxtbox.Text = i.LostValue.ToString();
                }
               
                //RMA Number

                if (i.RMANumber.ToString() == null)
                {
                    rmanumtxtbox.Text = "";
                }
                else
                {
                    rmanumtxtbox.Text = i.RMANumber.ToString();
                }
               

            }

The Lines in bold are the controls I'm having problems with. The OrigDate Text Box will populate but the other dates which have the same return typ as the OrigDate will not populate and there is data being returned when I look at the debug info the controls have the data there but it's not being displayed. My dropdownlist select a 'Y' or 'N' value will work on the lines not commented out ERROR: NullReferenceException was unhandled by user code. I cannot figure this out because the other bold dropdownlist will have null values and they are handled.

The last if else statement will give me a null reference error if there is not any data in the returned data, but the other if else statements will work if there is a null returned.
0
Comment
Question by:frtools
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:kumar754
ID: 35040629
does your stored procedure returns Null Dates ???

if so, then you may need to handle like this:

datecreatedtxtbox.Text = i.IncidentCreatedDate.HasValue ? i.IncidentCreatedDate.Value.ToString() : "";
0
 

Author Comment

by:frtools
ID: 35057316
I figured the dates and dropdown boxes out. It's the very last if(statement) error NullReferenceException was unhandled by user code, Object reference not set to an instance of an object.
0
 
LVL 12

Expert Comment

by:kumar754
ID: 35060951
try changing your code to:

if (i.RMANumber.HasValue)
{
   rmanumtxtbox.Text = i.RMANumber.Value;
}
else
{
   rmanumtxtbox.Text = "";
}                

0
 

Author Comment

by:frtools
ID: 35062332
That variable does not have that property.  The return type of the variable is a string. When a record is created if the RMAoption is == y it triggers a function to create a RMANumber varchar(15). Does that have something to do with the problem, it does not make sense since I'm just executing a select query on the database.
0
 
LVL 12

Accepted Solution

by:
kumar754 earned 500 total points
ID: 35062392
for string you shld always use: string.IsNullOrEmpty()

if (!string.IsNullOrEmpty(i.RMANumber))
{
   rmanumtxtbox.Text = i.RMANumber.ToString();
}
else
{
   rmanumtxtbox.Text = "";
}

or another shortcut:

rmanumtxtbox.Text = string.Format("{0}", i.RMANumber); // will hanlde null as well
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question