每当我单击表单上的提交按钮时,我都会收到以下错误消息:
错误:System.ArgumentNullException:值不能为空。参数名称:ClassesnWorkshops._Default.GetHousesBySearchCriteria() 中 Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) 中的 connectionString。. .
我是 C# 和 Visual Studio 的初学者。谁能告诉我为什么我的连接字符串可能为空?有什么想法可以解决这个错误吗?我使用项目属性的设置按钮创建了一个名为 connectionString 的 ConnectionString 连接到我的 Classes 数据库。但这可能是不必要的,因为它没有改变错误。
//Code for my submit button click event:
protected void cmdSearch_Click(object sender, EventArgs e)
{
DataTable objDT = null;
try
{
//Query the database for houses
objDT = GetHousesBySearchCriteria();
//Any houses found?
if (objDT.Rows.Count == 0)
{
//None found - hide repeater, show message
rptHouses.Visible = false;
lblMessage.Text = "No results found";
}
else
{
//Houses found - show repeater, hide message
rptHouses.DataSource = objDT;
rptHouses.DataBind();
rptHouses.Visible = true;
lblMessage.Text = "";
}
}
catch (Exception ex)
{
//Add your own error handling here
lblMessage.Text = "Error: " + ex.ToString();
}
finally
{
//Release memory
objDT = null;
}
}
#endregion
#region GetHousesBySearchCriteria
public DataTable GetHousesBySearchCriteria()
{
//Use the Microsoft Data Application Blocks to query database
DataSet objDS = new DataSet();
SqlParameter[] arParms = new SqlParameter[2];
arParms[0] = new SqlParameter("@Zipcode", SqlDbType.Char);
arParms[0].Value = txtZipCode.Text;
arParms[1] = new SqlParameter("@Miles", SqlDbType.Decimal);
arParms[1].Value = Int16.Parse(cboWithin.SelectedItem.Value);
lblTestParms0.Text = txtZipCode.Text;
lblTestParms1.Text = cboWithin.SelectedValue.ToString();
//Return a DataTable
return SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["ConnectionString"],
CommandType.StoredProcedure, "spHouses_GetNearZipcode", arParms).Tables[0];
}
#endregion
存储过程代码:
CREATE PROCEDURE [dbo].[spHouses_GetNearZipcode]
@Zipcode char(5),
@Miles decimal(11,6)
AS
--Load close zipcodes into temp table
SELECT ZIP.ZipCode, ZIP.City,
dbo.DistanceFunc(ZIP.Latitude, ZIP.Longitude, RAD.Latitude, RAD.Longitude) As Distance
INTO #TempZips
FROM ZipCodes ZIP, RadiusFunc(@ZipCode, @Miles) RAD
WHERE (ZIP.Latitude BETWEEN RAD.MinLatitude AND RAD.MaxLatitude) AND
(ZIP.Longitude BETWEEN RAD.MinLongitude AND RAD.MaxLongitude) AND
(dbo.DistanceFunc(ZIP.Latitude,ZIP.Longitude,RAD.Latitude,RAD.Longitude) <= @Miles)
--Search Houses table and JOIN to temp zipcodes table
SELECT H.*, Zips.Distance AS Miles
FROM Schools H INNER JOIN
#TempZips Zips ON Zips.ZipCode = H.zip
ORDER BY Zips.Distance
RETURN