Solved

Populate textbox in gridview based on selection from dropdownlist

Posted on 2011-03-24
10
596 Views
Last Modified: 2012-05-11
Hi Experts,

I'm populating a gridview using an objectdatasource (odsCommission) with methods from my business logic layer.

I added a dropdownlist in an edititemtemplate that is populated by another objectdatasource (odsAssignment). This populates the dropdownlist with counties.

When I click on edit the proper county is in place for that record and if I change it and click update, it functions as it should. Perfect!

Now I need to get the selected item value when the county is changed, passing to a stored procedure and have it return the District based on the counties selected value and put it in a label for column "Assigned District"


This is what I have so far.....
And I know it's probably the wrong approach but I'm trying to learn how to customize gridviews.

Thanks
 myCode.pdf
0
Comment
Question by:tis9700
  • 5
  • 5
10 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Hi,

I think there is nothing wrong with you code, the pourpose of the RowUpdating Event is that you can manipulate the values of the Row before the values are passed to the data source, so if you need to get a value related to the user selection, this is the way to go.

Your code have very good things like using Store Procedures and Using a SqlDataReader, so you are on the right path, because the alternative will be to use a trigger on your DB Table that updates the Assigned District Automatic when then County is updated.

But I am curious, what makes you think that there is a problem with your code?

0
 

Author Comment

by:tis9700
Comment Utility
Hi gamarroigg,

I think there is a problem because I get an error that I should have mentioned earlier...

NullReferenceException was caught.
Object reference not set to an instance of an object.

I set a break point on ..

cmd.Parameters.AddWithValue("@Countycode", ddl.SelectedItem.Value)

and am not able to get any value. Like maybe the value from the dropdownlist isn't being updated before the code to get the assigned district runs and returns that value. Nothing is sent to the @Countycode parameter.

If I weren't in a gridview, I would have the code fire in the dropdownlist selectedchanged index event. But I'm just learning my way around gridviews and have only found the most basic information on how to use them much less customize them.

Thanks
0
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Ok, if your code is exactly like the in PDF you post, your problem will be that in your markup, you DropDownList name is ddlAssignment, but in your code you are looking for ddlAssignments, so you have an extra S letter at the end of the name, so use the correct ID and try again
0
 

Author Comment

by:tis9700
Comment Utility
Well, that was part of it. I no longer get the error and the value shows up for ddlAssignment but it's not returning the correct district.

For example, Davidson county with a county code of "19" is in district "21". If I select Anderson county with a county code of "01" from the dropdownlist, I need it to return district "41" to lblDistrict but it's remains 21 though the correct county code is being passed to the stored procedure.

I tested my stored procedure. It returns the correct value of 41 and the name of the stored procedure and parameter are spelled correctly with case sensitive.

Thanks
0
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Ok,  put a break point in this line

lbl.Text = rd("District").ToString()

And see if the value returned by your Store Procedure is the correct one.

Just remember that you are assigning the Value returned from you Store Procedure to the lblDistrict control, but, since this control is a label, it would not  update your grid, and when the updating is finished this control it would be populated by te grid with the value that the AssignedDistrict column have.

If what you want is to update the AssignedDistrict field according to your Store Procedure returned value, you should chante the type of your lblDistrict control to TextBox or add some code that update the field of the grid datasource
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:tis9700
Comment Utility
Hi gamarrojgg,

I changed the the label to a textbox (txtDistrict).

The district is being returned  in
txt.text = rd("District").ToString()

but not updated in the record in the table or displayed in the gridview.

I need this value updated to the table when the row is updated. Is my row updating before I return the value?

What do you mean by "add some code that update the field of the grid datasource"?

Thanks
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
Comment Utility
hi, the rowupdating  events occurs just before the grid updates your table, it should be working, but perhaps there is something w have not see yet.

What is your grid datasource? a dataset? a datatable? a SqlDatasource?
0
 

Author Comment

by:tis9700
Comment Utility
Hi gamarrojgg,

I got it working by changing the following..

 RevisedCode.docx
0
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Great! just keep in mind  that every time the user change the DropDownList, a postback will occur, if that is not a problem for you, then it is ok.

There is anything else I can help you with?

0
 

Author Comment

by:tis9700
Comment Utility
Nope, you were great helping me through it.

Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

11 Experts available now in Live!

Get 1:1 Help Now