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
Solved

Populate textbox in gridview based on selection from dropdownlist

Posted on 2011-03-24
10
598 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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