Friday, March 30, 2012

parameter problem

Hi, i want to bind dropdowenlist ti gridview so i fill grid with aprameter takes from dropdownlist

i use this code to defien the parameter

SqlParameter pram1 =newSqlParameter("@.Group_ID",SqlDbType.Int, 4,"Group_ID");

pram1.Direction =ParameterDirection.Input;

pram1.Value =Convert.ToInt32(DropDownList5.SelectedItem.Value);

cmd.Parameters.Add(pram1);

what is th problem in value????

i try with many thing but the same result

plz help,,,

Hello my friend,

Use the following line: -

cmd.Parameters.Add("@.Group_ID", SqlDbType.Int, 4).Value = DropDownList5.SelectedItem.Value;

I would be sure to test that the dropdown list has a selected value or your code will crash: -

if (DropDownList5.SelectedIndex >= 0)

Kind regards

Scotty

|||

I agree. Something like:

cmd.Parameters.Add(
"@.Group_ID"
, SqlDbType.Int
).Value = (DropDownList5.SelectedIndex >= 0) ? DropDownList5.SelectedItem.Value : DBNull.Value;

Of course, your sql statement/sproc should consider @.Group_ID could be NULL as well.

|||

Try to use

DropDownList5.SelectedValue instead ofDropDownList5.SelectedItem.Value

Satya

|||

In order to know whether an items was selected (checking SelectedIndex >= 0) or not (setting the parameter to DBNull.Value) then you would be doing extra checks on SelectedIndex (check Reflector for the exact code). A minor point, but something to consider.

No comments:

Post a Comment