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

时间:2015-11-23 12:42:53

标签: c# asp.net

我想设置值" id"到会话[" p_id"],然后用于将其存储在另一个页面的其他位置。 它似乎是一个错误"不能将值NULL插入列' P_Id',table" 这是我的代码:

//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());
    }

1 个答案:

答案 0 :(得分:0)

我终于回答了我的问题。谢谢大家的贡献

  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());
        }
    }