Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a one web form.in this form i put one text box ,drop down list and one grid view. In gridview there is template field as a Linkbutton (Command Name="view"). so when I click On view button then selected data will be display on text box and dropdown list..
Iam writing following code on gridview row command Event But Still it Display Error. .

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      
        DataClassesDataContext db = new DataClassesDataContext();
        db.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Cn"].ConnectionString;
        if (e.CommandName == "view")
        {
            int StateId = Convert.ToInt32(e.CommandArgument);
            HiddenField1.Value = StateId.ToString();
            var state =
                (
                    from s in db.tblStates
                    where (s.Stateid == Convert.ToInt32(e.CommandArgument))
                    select new
                    {
                        s.StateName,
                        s.Countryid
                    }
                 ).FirstOrDefault();
            if (state != null)
            {
                TextBox1.Text = state.StateName;
                DropDownList1.SelectedValue = state.Countryid;
            }
                         
        }
    }




It Display Follwing Error..

C#
 System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.

Source Error: 


Line 78:             int StateId = Convert.ToInt32(e.CommandArgument);
Line 79:             HiddenField1.Value = StateId.ToString();
Line 80:             var state =
Line 81:                 (
Line 82:                     from s in db.tblStates

Thanx
Posted
Comments
Orcun Iyigun 22-Feb-13 7:00am    
During debugging your code have you checked if the value is null or a character or not?

problem lies in
(s.Stateid == Convert.ToInt32(e.CommandArgument))
linq does not support conveting to int while fetching data

try this,

where (s.Stateid ==StateId)
 
Share this answer
 
Comments
Hidayat Meman 22-Feb-13 7:13am    
thanx @akhtar..
but its not a problam.
problem is here
var state =
(
from s in db.tblStates
where (s.Stateid == Convert.ToInt32(e.CommandArgument))
select new
{
s.StateName,
s.Countryid
}
).FirstOrDefault();
Hidayat Meman 22-Feb-13 7:14am    
first time state becomes null and above error will be display
Taha Akhtar 22-Feb-13 7:21am    
I think first check your command argument value,it will be numeric,and 2nd above solution, did you try replcaing where
Hidayat Meman 22-Feb-13 7:38am    
command argument value also integer and also try your 2nd way. i try to code another way check it... its correct or not ? ??

var state =
from s in db.tblStates
where (s.Stateid == StateId)
select s;
foreach (tblState st in state)
{
TextBox1.Text = st.StateName;
DropDownList1.SelectedValue = st.Countryid;
}

//it also display same error
Taha Akhtar 22-Feb-13 7:47am    
what is the value of st.Countryid
I think first check your command argument value,it will be numberic,and 2nd above solution
 
Share this answer
 

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