T
TurkiaGL
Guest
Hello , can you please ? i have error executing my sql query below and i did not how to fix it?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Title,TableName,IdTab")] SsmsColumn ssmsColumn, string database, string nomtable)
{ try
{
if (ModelState.IsValid)
{
ssmsColumn.Id = Guid.NewGuid();
_context.Add(ssmsColumn);
string primaryKey;
string noNull;
string colName = ssmsColumn.Title;
string TableName = nomtable;
string datatype = ssmsColumn.DataType;
int? maxChar = ssmsColumn.CharacterMax;
string charM = maxChar.ToString();
bool Key = ssmsColumn.PrimaryKey.HasValue ? ssmsColumn.PrimaryKey.Value : false;
bool nullable = ssmsColumn.IsNullable.HasValue ? ssmsColumn.IsNullable.Value : true;
if (Key)
{ primaryKey = "PRIMARY KEY"; }
else { primaryKey = ""; }
if (nullable)
{ noNull = "null"; }
else { noNull = "Not Null"; }
string sql = " Use " + database + "; CREATE TABLE " + TableName + " (" + colName + " "+datatype+"(" + maxChar + ")" + primaryKey + " " + noNull +");";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["IdTab"] = new SelectList(_context.SsmsTable, "Id", "Title", ssmsColumn.IdTab);
DataSet ds = dblayer.Get_Instance();
ViewBag.database = ds.Tables[0];
}
catch (DbUpdateException /* ex */)
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists " +
"see your system administrator.");
}
//DataSet ds = dblayer.Get_Datatype();
Continue reading...
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Title,TableName,IdTab")] SsmsColumn ssmsColumn, string database, string nomtable)
{ try
{
if (ModelState.IsValid)
{
ssmsColumn.Id = Guid.NewGuid();
_context.Add(ssmsColumn);
string primaryKey;
string noNull;
string colName = ssmsColumn.Title;
string TableName = nomtable;
string datatype = ssmsColumn.DataType;
int? maxChar = ssmsColumn.CharacterMax;
string charM = maxChar.ToString();
bool Key = ssmsColumn.PrimaryKey.HasValue ? ssmsColumn.PrimaryKey.Value : false;
bool nullable = ssmsColumn.IsNullable.HasValue ? ssmsColumn.IsNullable.Value : true;
if (Key)
{ primaryKey = "PRIMARY KEY"; }
else { primaryKey = ""; }
if (nullable)
{ noNull = "null"; }
else { noNull = "Not Null"; }
string sql = " Use " + database + "; CREATE TABLE " + TableName + " (" + colName + " "+datatype+"(" + maxChar + ")" + primaryKey + " " + noNull +");";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["IdTab"] = new SelectList(_context.SsmsTable, "Id", "Title", ssmsColumn.IdTab);
DataSet ds = dblayer.Get_Instance();
ViewBag.database = ds.Tables[0];
}
catch (DbUpdateException /* ex */)
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists " +
"see your system administrator.");
}
//DataSet ds = dblayer.Get_Datatype();
Continue reading...