Hi 65sametkaya65,
Please refer below sample code.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
Namespaces
using System.Configuration;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string qry = "SELECT MAX(CustomerId) FROM Customers";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(qry, con))
{
con.Open();
ViewBag.Id = (Convert.ToInt32(cmd.ExecuteScalar()) + 1).ToString();
con.Close();
}
}
return View();
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
ID:
@ViewBag.Id
</body>
</html>
Output
ID: 5