我有两个DROP DOWN LIST:一个用于PROVINCE,另一个用于CITY连接到DB。 我希望能够从第一个下拉列表中选择一个PROVINCE,并且能够看到 特别省的城市在第二个下拉列表中。 但是我收到错误“无法将参数值从字符串转换为Int32”。在线“DataSet ds1 = new DataSet();”。
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 --------------------------------------------------
我的代码:
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()
// {
// }
}
}