将变量设置为会话状态时出错

本文关键字:出错 会话状态 变量 设置 | 更新日期: 2023-09-27 18:24:06

我想将值"id"设置为会话["p_id"],然后使用将其存储在另一个页面的其他位置。出现错误"无法将值NULL插入表的列"P_Id"中"这是我的代码:

//Sample of code from an .aspx page codebehind. Here the value id is retrieved and saved in a Session state.   
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string pat_id = "SELECT P_id FROM P_identity WHERE Amka = @amka ";
SqlCommand com2 = new SqlCommand(pat_id, conn);
com2.Parameters.AddWithValue("@amka",TextBoxAmka.Text);
int id = Convert.ToInt32(com2.ExecuteScalar());
 Session["pa_id"] = id;//here i think the problem is
[...]

[...] //another .aspx page codebehind that i want to pass the Session state from the other page, and insert it to the database

protected void btndias_Click(object sender, EventArgs e)
{
    try
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
        conn.Open();
int id = Convert.ToInt32(Session["pa_id"]);
        string insert_dim1 = "insert into P_dimensions (P_Id,Height,Weight) values (@pa_id, @height, @weight) ";
        SqlCommand com = new SqlCommand(insert_dim1, conn);
        com.Parameters.AddWithValue("@pa_id", id);
        com.Parameters.AddWithValue("@height", txbdi2.Text);
        com.Parameters.AddWithValue("@weight", txbdi1.Text);
        com.ExecuteNonQuery();
        if (note_di.Text.Length != 0)
        {
            string insert_dim2 = "insert into P_dimensions (Note_dim) values (@note_dim) ";
            SqlCommand com2 = new SqlCommand(insert_dim2, conn);
            com2.Parameters.AddWithValue("@note_dim", note_di.Text);
            com2.ExecuteNonQuery();
        }
        Response.Write("<script>alert('Τα στοιχεία αποθηκεύτηκαν επιτυχώς!')</script>");
        conn.Close();
    }
    catch (Exception ex)
    {
        Response.Write("Error :" + ex.ToString());
    }

将变量设置为会话状态时出错

我终于回答了我的问题。感谢大家对的贡献
  protected void btndias_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            int id = Convert.ToInt32(Session["pa_id"]);

            if (note_di.Text.Length != 0)
            {
                string insert_dim2 = "insert into P_dimensions (P_Id,Height,Weight,Note_dim) values (@pa_id, @height, @weight,@note_dim) ";
                SqlCommand com2 = new SqlCommand(insert_dim2, conn);
                com2.Parameters.AddWithValue("@pa_id", id);
                com2.Parameters.AddWithValue("@height", txbdi2.Text);
                com2.Parameters.AddWithValue("@weight", txbdi1.Text);
                com2.Parameters.AddWithValue("@note_dim", note_di.Text);
                com2.ExecuteNonQuery();
            }
            else
            {
                string insert_dim1 = "insert into P_dimensions (P_Id,Height,Weight) values (@pa_id, @height, @weight) ";
                SqlCommand com = new SqlCommand(insert_dim1, conn);
                com.Parameters.AddWithValue("@pa_id", id);
                com.Parameters.AddWithValue("@height", txbdi2.Text);
                com.Parameters.AddWithValue("@weight", txbdi1.Text);
                com.ExecuteNonQuery();
            }

            Response.Write("<script>alert('Τα στοιχεία αποθηκεύτηκαν επιτυχώς!')</script>");
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Error :" + ex.ToString());
        }
    }