0

SQL LITE次のようにデータベースにデータを挿入するコードを書きました

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        count = 0;
       Session["x"] = "session value";

    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    string path = Server.MapPath("bin/sampldb.db");
    SQLiteConnection conn = new SQLiteConnection("Data Source=" + path + "");
    try
    {
        conn.Open();
        SQLiteCommand cmd = new SQLiteCommand();
        cmd.Connection = conn;
        string txt="insert into stu values("+TextBox1.Text  +",'"+TextBox2.Text+"')";
        cmd.CommandType = CommandType.Text;
        cmd.CommandText  = txt;
        cmd.ExecuteNonQuery();
        conn.Close(); 
    }
    catch (Exception ex)
    {
        Label1.Visible = true;
        Label1.Text = "Error:" + ex.Message;
    }

}

Sessionデータの取得中に挿入した後null、理由がわかりません

 protected void Button2_Click(object sender, EventArgs e)
{
    if (Session["x"] != null) // Getting Null here
    {
        Label1.Visible = true;
        Label1.Text = Session["x"].ToString();
        DataSet m_oDataSet = new DataSet();
        string path = Server.MapPath("bin/sampldb.db");
        SQLiteConnection conn = new SQLiteConnection("Data Source=" + path + "");
        try
        {
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string txt = "select * from stu";
            cmd.CommandText = txt;

            SQLiteDataAdapter adp = new SQLiteDataAdapter();
            adp.SelectCommand = cmd;
            adp.Fill(m_oDataSet);
            GridView1.DataSource = m_oDataSet.Tables[0];
            GridView1.DataBind();

        }
        catch (Exception ex)
        {
            Label1.Visible = true;
            Label1.Text = "Error:" + ex.Message;
        }
        finally
        {
            conn.Close();
        }

    }
}

でテストしたときの同じコードは正常にSql server 2008機能します

protected void BtnSqlInsert_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(cnstr);
    con.Open();
    SqlCommand cmd = new SqlCommand("Insert into [User] values ('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
    cmd.ExecuteNonQuery();
    con.Close();
}
protected void BtnSqlGet_Click(object sender, EventArgs e)
{
    if (Session["x"] != null) //Able to get session here
    {
        Label1.Visible = true;
        Label1.Text = Session["x"].ToString();
    }
}

私のsql liteパスはBin画像に示されているようにフォルダからです

ここに画像の説明を入力してください

4

1 に答える 1

1

アプリはDB(BINフォルダーにあります)に書き込みます。これにより、アプリの再起動=>in-procセッションが失われます。(StateServerモードを使用して)結果を解決するべきではありません。元の理由を修正する必要があります。dbファイルをBINフォルダーから離れた別のフォルダーに移動します。

于 2012-04-11T12:14:46.370 に答える