我的数据库中有3个表"tbl_User", "tbl_City", "tbl_Town"
。
在"tbl_City"
,我有
- cityno int [PK],
- cityname nvarchar(50)
区域。
在"tbl_Town"
,我有
- townno int,
- townname nvarchar(50),
- cityno int
区域。
在"tbl_User"
,我有
- userid int [PK],
- email nvarchar(50),
- password nvarchar(50),
- city int,
- town int
区域。
当用户在网站上注册时,他必须选择城市和城镇。所以我可以将城市和城镇保存为"tbl_User"
中的号码。我想要做的是:当用户转到“profile.aspx”时,我希望有选择地在DropDownList中看到城市和城镇。我的“profile.aspx.cs”代码如下:
private void GetCityAndTownSelectively()
{
if (Session["userid"] != null)
{
DataRow dr = function.GetDataRow("SELECT tbl_City.cityno, tbl_City.cityname, tbl_Town.townno, tbl_Town.townname FROM tbl_User LEFT JOIN tbl_City on tbl_City.cityno = tbl_User.city LEFT JOIN tbl_Town on tbl_Town.townno = tbl_User.town WHERE userid=" + Session["userid"].ToString());
if (dr == null)
{
Response.Redirect("default.aspx");
}
else
{
DrpDwnLstCity.ClearSelection();
DrpDwnLstCity.Items.FindByValue(dr["cityno"].ToString()).Selected = true;
DrpDwnLstTown.ClearSelection();
DrpDwnLstTown.Items.FindByValue(dr["townno"].ToString()).Selected = true;
}
}
else
{
Response.Redirect("default.aspx");
}
}
我更改了我的代码,但我仍然收到错误:aytasarim.dll中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理。
答案 0 :(得分:0)
您的Sql查询错误
SELECT tbl_City.cityname, tbl_Town.townname FROM tbl_User
LEFT JOIN tbl_City on tbl_City.cityno = tbl_User.city
LEFT JOIN tbl_Town on tbl_Town.townno = tbl_User.town
WHERE userid="";
由于cityno
表中没有tbl_User
,并且您在1个表中具有值int的列上创建join
,而在其他表中具有Nvarchar
答案 1 :(得分:0)
尝试以下
private void GetCityAndTownSelectively()
{
if (Session["userid"] != null)
{
DataRow dr = function.GetDataRow("SELECT tbl_City.cityno, tbl_City.cityname, tbl_Town.townno, tbl_Town.townname FROM tbl_User LEFT JOIN tbl_City on tbl_City.cityno = tbl_User.city LEFT JOIN tbl_Town on tbl_Town.townno = tbl_User.town WHERE userid=" + Session["userid"].ToString());
if (dr == null)
{
Response.Redirect("default.aspx");
}
else
{
DrpDwnLstCity.ClearSelection();
DrpDwnLstCity.Items.FindByValue(dr["cityno"].ToString()).Selected = true;
DrpDwnLstTown.ClearSelection();
DrpDwnLstTown.Items.FindByValue(dr["townno"].ToString()).Selected = true;
}
}
else
{
Response.Redirect("default.aspx");
}
}