0
votes

I am recieving the following error message whenever I click the submit button on my form:

Error: System.ArgumentNullException: Value cannot be null. Parameter name: connectionString at Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) at ClassesnWorkshops._Default.GetHousesBySearchCriteria() . . .

I am a beginner with c# and visual studio. Can anyone tell me why my connection string might be null? Any ideas how I might resolve this error? I used the Settings button of the project Properties to create a ConnectionString called connectionString connecting to my Classes database. but this may have been unnecessary, as it did not change the Error.

//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

stored procedure code:

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
2
If you called it "connectionString", maybe try using that instead of "ConnectionString" in the AppSettings. I'm pretty sure something like that would be case sensitive. - Jon La Marr
Thanks for the suggestion. Unfortunately, that did not work either. - Reesse
Here is the connectionString code in my web.config file. Do you think that anything here might be the problem? I don't even know why there is SQLEXPRESS info here.<connectionStrings> - Reesse
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> <add name="ClassesnWorkshops.Properties.Settings.ConnectionString" connectionString="Data Source=TERESA-PC;Initial Catalog=Classes;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> - Reesse

2 Answers

0
votes

I think the ConfigurationManager argument in your ExecuteDataset should look like this:

return SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["ConnectionString"].ConnectionString, CommandType.StoredProcedure, "spHouses_GetNearZipcode", arParms).Tables[0];

Add ".ConnectionString" after your AppSettings key

0
votes

When using ConfigurationManager.AppSettings the key values are case sensitive. Is your connection string named

connectionString or ConnectionString
^                   ^