Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
postgdetails.Qua = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).SelectedItem.ToString();

where postgdetails is a object of a class.
when debug this code it throw a exception in this line .exception are given below

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.DropDownList'.


while
postgdetails.Medium = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).SelectedItem.Text.ToString();

this line is working properly in same program how is that.

plz tell me about that problem.
Posted

1 solution

The run-time types of the cell contents are different for the different cells. One type is DropDownList or assignment-compatible with it, another type is not. When you dynamically down-cast it to an incompatible type, it throws this exception. Check up what is actually in the cell in question. As simple as that.

Generally, it can be good to check up a run-time type instead of blind-folded type casting. Something like that:
C#
DropDownList list = GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as DropDownList;
if (list != null) // successful cast
   postgdetails.Qua = list.SelectedItem.ToString();
else
   MessageBox.Show("Oh no! Not again! :-)");

—SA
 
Share this answer
 
v2
Comments
srishti_ 6-Jul-12 3:53am    
what should do for that in coding
Sergey Alexandrovich Kryukov 6-Jul-12 3:56am    
Didn't I just answer this question?
Oh, I see, probably you asked before you saw v2 of the answer...
--SA
Abhinav S 6-Jul-12 3:56am    
5 of course.
Sergey Alexandrovich Kryukov 6-Jul-12 3:57am    
Thank you, Abhinav.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900