[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Populate ASP Controls w/ Stored Procedure Return Results Failure

Posted on 2011-03-04
5
Medium Priority
?
294 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 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 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