Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populate ASP Controls w/ Stored Procedure Return Results Failure

Posted on 2011-03-04
5
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

609 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