Solved

Populate ASP Controls w/ Stored Procedure Return Results Failure

Posted on 2011-03-04
5
280 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now