Hi Friends,
I have the below code where I am using a connection, sqlcommand, and sqldatareader to retrieve records from SQL Server. My question is I want to use the sqldatareader to loop through the returned results as you see I have a while loop below in the code section. My question is how would you recommend integrating this while loop into the code below it. Thanks !!!
while (rdr.Read())
{
var value= rdr["DataBaseName"];
}
public ActionResult ShowSSN(ShowSSN.Models.ShowSSNModel model)
{
string message;
string status;
if (ModelState.IsValid)
{
using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[model.connectionString].ConnectionString))
using (var command = new SqlCommand("[dbo].[spGetSSN]", connection)
using (SqlDataReader rdr = sqlCommand.ExecuteReader())
{
CommandType = CommandType.StoredProcedure
})
{
try
{
connection.Open();
command.Parameters.Add(new SqlParameter("ssn", model.SSN));
//message = "SSN successfully deleted.";
//status = "success";
}
catch (SqlException e)
{
message = e.Message.ToString();
status = "danger";
}
finally
{
connection.Close();
}
}
return View(model);
}
else
{
message = "Invalid fields entered.";
status = "warning";
}
TempData["message"] = message;
TempData["status"] = status;
return RedirectToAction("", "ShowSSN");
}