0

I have to bind gridview by selecting the drop down list. While using the given below code generate one error. The error is :" cannot convert from 'string' to int". How can I solve this issue. Please Help me.

Code:

protected void ddlVersionNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShadingAnalysisDataSetTableAdapters.tbl_ShadingAnalysisTableAdapter adp1;
        adp1 = new ShadingAnalysisDataSetTableAdapters.tbl_ShadingAnalysisTableAdapter();
        DataTable dt = new DataTable();
        dt = adp1.GetGridData(ddlSiteID.SelectedValue, ddlVersionNo.SelectedValue); //error is shown here
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

SQL Query:

SELECT Site_ID, version_number,A,B FROM tbl_ShadingAnalysis WHERE Site_ID=@Site_ID AND version_number=@version_number

Data Base:

enter image description here

Vipin
  • 261
  • 6
  • 20
  • 44
  • 1
    On which line exactly? What is your `GetGridData` method do? – Soner Gönül Oct 15 '14 at 12:30
  • 1
    can you at least give us a hint which line / etc reports this? I'm *assuming* that it is the `GetGridData`; so: what is the signature of that method? `DropDownList.SelectedValue` is `string`... – Marc Gravell Oct 15 '14 at 12:31

2 Answers2

2

I can only guess from the image that GetGridData has the signature:

DataTable GetGridData(string siteId, int versionNumber)

in which case, since DropDownList.SelectedValue is string, you'll need to parse:

DataTable dt = adp1.GetGridData(
    ddlSiteID.SelectedValue, int.Parse(ddlVersionNo.SelectedValue));
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • `VerionNumber` is nullable, so the second parameter is a `int?`(the title also suggests it). – Tim Schmelter Oct 15 '14 at 12:36
  • +1 That seems to be the logical deduction from the information provided – Mrinal Kamboj Oct 15 '14 at 12:36
  • @TimSchmelter the user posted SQL with a `version_number=@version_number` - so either the parameter is non-nullable, or broken ;p – Marc Gravell Oct 15 '14 at 12:37
  • @ Marc Gravell user seems to be trying to convert string to int?, as I can understand from the question, so need to do something like what Jon suggests in following link - http://stackoverflow.com/questions/16851527/tryparse-nullable-types-generically – Mrinal Kamboj Oct 15 '14 at 12:42
  • @MarcGravell: maybe he just wants to find records with a given `version_number` and it's not possible that `null` is passed. – Tim Schmelter Oct 15 '14 at 12:45
1

You need to format your Site_ID as an "int" or make a new Primary Key.

 SELECT Site_ID, version_number,A,B FROM tbl_ShadingAnalysis WHERE Site_ID=@Site_ID AND version_number=@version_number

Data Base needs to have a Primary Key that is an Integer!

ematica
  • 51
  • 2
  • 10