System.Data.SqlClient.SqlException (0x80131904): Timeout expired occured after i update the stored procedure

1.5k Views Asked by At

I get an error:

System.Data.SqlClient.SqlException (0x80131904): Timeout expired.

in my C# application. The problem occurs after I updated the stored procedure in the database.

Here's my c# code.

try
{
    string connectionString = ConfigurationManager.ConnectionStrings["dsmmdb"].ToString();
    connection = new SqlConnection(connectionString);

    //Build query
    string sql = string.Format("EXEC [DSMMTracker_ACQ_GetGeoByIp] @IpAddress = N'{0}'", serverVarRemoteAddr);
    log.Debug(sql);

    connection.Open();
    SqlCommand cmd = new SqlCommand(sql, connection);
    //Execute DSMMTracker_ACQ_GetGeoByIp stored procedure
    var geoReader = cmd.ExecuteReader();

    while (geoReader.Read())
    {
        target.Geo = ACQConfig.PREFIX_TOKEN_GEO_COUNTRY + geoReader["CountryCode"].ToString().ToLower() + "+";
        target.Geo += ACQConfig.PREFIX_TOKEN_GEO_CITY + geoReader["CityName"] == null ? "" : ACQConfig.PREFIX_TOKEN_GEO_CITY + geoReader["CityName"].ToString().ToLower() + "+";
        target.Geo += ACQConfig.PREFIX_TOKEN_GEO_REGION + geoReader["RegionName"] == null ? "" : ACQConfig.PREFIX_TOKEN_GEO_REGION + geoReader["RegionName"].ToString().ToLower();
        log.Debug("Country Code: " + geoReader["CountryCode"].ToString().ToLower());
        log.Debug("City Name: " + geoReader["CityName"].ToString().ToLower());
        log.Debug("Region Name: " + geoReader["RegionName"].ToString().ToLower());
        target.Geo = target.Geo.TrimEnd('+');
    }
} 
catch (Exception ex)
{
   log.Error("Error retrieving geo information from IP address: " + ex.ToString());
}
finally
{
   if(connection != null && connection.State == System.Data.ConnectionState.Open)
     connection.Close();
}

The stored procedure runs properly in SQL Server Management Studio.

The error trigger when I run the C# code.

Thanks.

1

There are 1 best solutions below

0
Prashant2329 On

Use

string connectionString = ConfigurationManager.ConnectionStrings["dsmmdb"].ConnectionString;

instead of:

string connectionString = ConfigurationManager.ConnectionStrings["dsmmdb"].ToString();