如何在Visual Studio 2010中基于connectionString修复SystemArgumentNull
本文关键字:connectionString 修复 SystemArgumentNull 2010 Visual Studio | 更新日期: 2023-09-27 18:37:16
每当我单击表单上的提交按钮时,我都会收到以下错误消息:
错误: System.ArgumentNullException: 值不能为空。参数名称:连接字符串位于 Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) at ClassesnWorkshops._Default.GetHousesBySearchCriteria()
. . .
我是 c# 和 Visual Studio 的初学者。谁能告诉我为什么我的连接字符串可能为空?任何想法如何解决此错误?我使用项目"属性"的"设置"按钮创建了一个名为"connectionString"的连接字符串,连接到我的类数据库。但这可能是不必要的,因为它没有改变错误。
//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
我认为ExecuteDataset中的ConfigurationManager参数应该如下所示:
return SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["ConnectionString"].ConnectionString, CommandType.StoredProcedure, "spHouses_GetNearZipcode", arParms).Tables[0];
加上".连接字符串",在应用设置键之后
使用ConfigurationManager.AppSettings
时,键值区分大小写。连接字符串是否命名
connectionString or ConnectionString
^ ^