0

我有两个下拉列表:一个用于省,一个用于连接到数据库的城市。我希望能够从第一个下拉列表中选择一个省,并能够在第二个下拉列表中看到该特殊省份的城市。但我收到错误“无法将参数值从字符串转换为 Int32”。在“DataSet ds1 = new DataSet();”行上。

在此处输入图像描述

我的数据库:
--------
_省
Prv_Id --- int (P.key)
Prv_Name --- nvarchar
--------------
_城市
Cty_Id --- int (P.key)
Cty_Name --- nvarchar
Cty_Prv_Id --- int (F.key)
----------------------------
----------------------------


我的 ST. 程序:
----------------------
更改程序 [dbo].[_City_selectItems] (@vari int)

作为
开始
SELECT * FROM _City WHERE (Cty_Prv_Id=@vari)
结尾

--------------------------------------------------
更改程序 [dbo].[_Province_selectAll]

作为
开始
选择 * FROM _Province
结尾
--------------------------------------------------

我的代码:

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()
       // {

      //  }


    }
}
4

0 回答 0