0

I have two DROP DOWN LISTs :one for PROVINCE and one for CITY which are connected to a DB. I want to be able to select a PROVINCE from the first drop down list and be able to see the CITIES of that special PROVINCE in the second drop down list. But I receive the error "Failed to convert parameter value from a String to a Int32." on line "DataSet ds1 = new DataSet();".

enter image description here

My DB:
--------
_Province
Prv_Id --- int (P.key)
Prv_Name --- nvarchar
--------------
_City
Cty_Id --- int (P.key)
Cty_Name --- nvarchar
Cty_Prv_Id --- int (F.key)
----------------------------
----------------------------


My ST.Procedures:
-----------------------
ALTER PROCEDURE [dbo].[_City_selectItems] (@vari int)

AS
BEGIN
SELECT * FROM _City WHERE  (Cty_Prv_Id=@vari)
END

--------------------------------------------------
ALTER PROCEDURE [dbo].[_Province_selectAll]

AS
BEGIN
SELECT * FROM _Province
END
--------------------------------------------------

MY CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication3_DrpDwnListAndSQL
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                func1();
                func2();
                //func3();
            }
        }

        protected void DrpDwnProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Text =DrpDwnProvince.SelectedValue;
            //string a = DrpDwnProvince.SelectedValue;
        }




        //First function for Province DrpDwnList 
        private void func1()
        {
            SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DrpBoxDB;Data Source=ARASH-PC");
            SqlDataAdapter com = new SqlDataAdapter("_Province_selectAll", con);
            com.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            if (com != null)
            { com.Fill(ds); }

            DrpDwnProvince.DataSource = ds;
            DrpDwnProvince.DataTextField = "Prv_Name";
            DrpDwnProvince.DataValueField = "Prv_Id";
            DrpDwnProvince.DataBind();
        }

        //Second function for City DrpDwnList
        private void func2()
        {
            SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DrpBoxDB;Data Source=ARASH-PC");
            SqlDataAdapter com1 = new SqlDataAdapter("_City_selectItems", con);
            com1.SelectCommand.Parameters.Add("@vari",SqlDbType.Int).Value=Label1.Text;

            com1.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataSet ds1 = new DataSet();
            if (com1 != null)
            { com1.Fill(ds1); }

            DrpDwnCity.DataSource = ds1;
            DrpDwnCity.DataTextField = "Cty_Name";
            DrpDwnCity.DataValueField = "Cty_Id";
            DrpDwnCity.DataBind();
        }

        //Third function for District DrpDwnList
       // private void func3()
       // {

      //  }


    }
}
arash deilami
  • 25
  • 1
  • 1
  • 7
  • What is the value of Label1.Text at the point of the exception. Since you are running this code on page load your label1.text might not have been initialized yet. – Ross Bush Apr 10 '13 at 20:17
  • Label1 was only for checking – arash deilami Apr 10 '13 at 20:20
  • actually i'm not sure about what i'm doing – arash deilami Apr 10 '13 at 20:21
  • After the DrpDwnProvince.DataBind() does it aoutomically select your index that would cause your IndexChange event to fire. You may need to check if the DrpDwnProvince.Items.Count>0 and set SelectedIndex=0 to force Label1.Text to load. – Ross Bush Apr 10 '13 at 20:40

0 Answers0