Solved

Populate ASP Controls w/ Stored Procedure Return Results Failure

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 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

20 Experts available now in Live!

Get 1:1 Help Now