Solved

Populate textbox in gridview based on selection from dropdownlist

Posted on 2011-03-24
10
600 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
[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
  • 5
  • 5
10 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35220924
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
ID: 35228170
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
ID: 35228264
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tis9700
ID: 35228350
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
ID: 35228400
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
 

Author Comment

by:tis9700
ID: 35232040
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
ID: 35232892
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
ID: 35233103
Hi gamarrojgg,

I got it working by changing the following..

 RevisedCode.docx
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35233731
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
ID: 35233956
Nope, you were great helping me through it.

Thanks
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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