here is my UPDATE page code.
using Microsoft.AspNetCore.Mvc;
usding Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;
namespace WebApplication2.Pages.Users
{
public class EditModel : PageModel
{
public UserInfo userInfo = new UserInfo();
public String errorMessage = "";
public String successMessage = "";
public void OnGet()
{
String id=Request.Query["id"];
try
{
String connectionString = "Data Source=DESKTOP-5406L1M;Initial Catalog=crud;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String sql = "SELECT * FROM users WHERE id=@id";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@id", id);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
userInfo.id = "" reader.GetInt32(0);
userInfo.name = reader.GetString(1);
userInfo.email = reader.GetString(2);
userInfo.phone = reader.GetString(3);
userInfo.address = reader.GetString(4);
}
}
}
}
}
catch(Exception ex)
{
errorMessage = ex.Message;
}
}
public void OnPost()
{
userInfo.id = Request.Form["id"];
userInfo.name = Request.Form["name"];
userInfo.email = Request.Form["email"];
userInfo.phone = Request.Form["phone"];
userInfo.address = Request.Form["address"];
if (userInfo.id.Length==0 ||userInfo.name.Length == 0 || userInfo.email.Length == 0 || userInfo.phone.Length == 0 || userInfo.address.Length == 0)
{
errorMessage = "All the field are required";
return;
}
try
{
String connectionString = "Data Source=DESKTOP-5406L1M;Initial Catalog=crud;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String sql ="UPDATE users "
"SET name=@name, email=@email, phone=@phone, address=@address "
"WHERE id=@id";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@name", userInfo.name);
command.Parameters.AddWithValue("@email", userInfo.email);
command.Parameters.AddWithValue("@phone", userInfo.phone);
command.Parameters.AddWithValue("@address", userInfo.address);
command.Parameters.AddWithValue("@id", userInfo.id);
command.ExecuteNonQuery();
}
}
}
catch(Exception ex)
{
errorMessage=ex.Message;
return;
}
Response.Redirect("/Users/Index");
}
}
}
` In the update page i got error
**Conversion failed when converting the nvarchar value '=2' to data type int. ** can anyone help me how to fix it?
CodePudding user response:
You got this error because you tried to convert column DataType from String to int which is not right. You may try to cast the ID
as Integer like
CAST(@ID AS int)
or
CONVERT(int,@ID)
CodePudding user response:
**Conversion failed when converting the nvarchar value '=2' to data type int. ** can anyone help me how to fix it?
Well, the exact mistake has been done in public void OnPost()
method on line userInfo.id = Request.Form["id"];
as it seems your userInfo.id
is a type of int
Therefore, Request.Form["id"]
will consider as string
so you outght to parse it into userInfo.id = Convert.ToInt32(Request.Form["id"]);
.
Nonetheless, userInfo.id.Length == 0
this operation is invalid on type int
instead you should write userInfo.id == 0
in your if condition
.
Output:
I have tested your code keeping id
as string
in the Model class
that is Request.Form["id"]
and its working as expected without any error. As you can see the below: So assuming value =2
has inserted mistakenly.