我試圖在Visual Studio 2015(社區版)中保存來自Web窗體的數據。我一再得到錯誤:在向表中添加數據時在com.ExecuteNonQuery()中獲取錯誤
no mapping exists from object type. Error at line "com.ExecuteNonQuery()".
我已經嘗試了各種解決方案在此論壇中提到,但他們都沒有爲我工作。請幫忙。謝謝。
錯誤消息:
我的代碼
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string checkuser = "select count(*) from [Table] where Username='" + un.Text + "'";
SqlCommand com = new SqlCommand(checkuser, conn);
temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("User already exists");
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (temp == 0)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string insertquery = "insert into [Table] (Designation, Username, Email, [Password]) values (@Designation, @Username, @Email, @Password)";
SqlCommand com = new SqlCommand(insertquery, conn);
com.Parameters.AddWithValue("@Designation", dn.SelectedItem.ToString());
com.Parameters.AddWithValue("@Username", un.Text);
com.Parameters.AddWithValue("@Email", em.Text);
com.Parameters.AddWithValue("@Password", pw.Text);
com.ExecuteNonQuery();
Response.Redirect("Managers.aspx");
Response.Write("Registration Successful");
conn.Close();
}
catch (Exception ex)
{
Response.Write("error :" + ex.ToString());
}
}
}
}
謝謝你,這對我有效。 –
歡迎您:) –