Как исправить ошибку "в hrms возникло исключение типа 'system .nullreferenceexception'.dll, но не обрабатывается в пользовательском коде
мой код выглядит примерно так
[HttpPost] public FileResult DownloadFile(int? fileId) { byte[] bytes; string fileName, contentType; string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "SELECT Name, Data, ContentType FROM tblFiles WHERE Id=@Id"; cmd.Parameters.AddWithValue("@Id", fileId); cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { sdr.Read(); bytes = (byte[])sdr["Data"]; contentType = sdr["ContentType"].ToString(); fileName = sdr["Name"].ToString(); } con.Close(); } } return File(bytes, contentType, fileName); } private static List<FileModel> GetFiles() { List<FileModel> files = new List<FileModel>(); string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("SELECT Id, Name FROM tblFiles")) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { files.Add(new FileModel { Id = Convert.ToInt32(sdr["Id"]), Name = sdr["Name"].ToString() }); } } con.Close(); } } return files; } } } The error is on string constr=ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; What I have tried: <pre>my code looks like this <pre> [HttpPost] public FileResult DownloadFile(int? fileId) { byte[] bytes; string fileName, contentType; string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "SELECT Name, Data, ContentType FROM tblFiles WHERE Id=@Id"; cmd.Parameters.AddWithValue("@Id", fileId); cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { sdr.Read(); bytes = (byte[])sdr["Data"]; contentType = sdr["ContentType"].ToString(); fileName = sdr["Name"].ToString(); } con.Close(); } } return File(bytes, contentType, fileName); } private static List<FileModel> GetFiles() { List<FileModel> files = new List<FileModel>(); string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("SELECT Id, Name FROM tblFiles")) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { files.Add(new FileModel { Id = Convert.ToInt32(sdr["Id"]), Name = sdr["Name"].ToString() }); } } con.Close(); } } return files; } } } The error is on string constr=ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;