0

私は ASP.NET C# Web サイトで作業しています (私はかなり新しいものです)、テキスト ボックスを使用して SQL データベースを更新する際に問題が発生しています。

ページの読み込み時にデータベースが空の場合はテキスト ボックスでデータベースを更新できますが、最初にデータベースからテキスト ボックスに SQL データをドラッグするコードを追加すると、送信時にデータを変更できません。

protected void updatebutton_Click(object sender, EventArgs e)
    {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["overlandconnectionstring2"].ConnectionString);
            con.Open();
            string insCmd = "UPDATE Stock SET [make] = @make, [model] = @model where SID ='" + Label1.Text + "'";

            SqlCommand editUser = new SqlCommand(insCmd, con);

            editUser.Parameters.AddWithValue("@make", make.Text);
            editUser.Parameters.AddWithValue("@model", model.Text);

            try
            {

                editUser.ExecuteNonQuery();

                con.Close();
                Response.Redirect("../stockkey1.aspx");


            }
            catch (Exception)
            {
                Response.Write("<br/>Something really bad happened, Please try again<br/>");
            }

            finally
            {
                con.Close();
            }
        }

正常に動作する更新コードですが、これをページの読み込みに追加すると動作しません。

protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["New"] != null)
        {

            Label2.Text = Session["New"].ToString();


            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["overlandconnectionstring2"].ConnectionString);
            con1.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select * from Stock where SID='" + Label1.Text + "'", con1);
            myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                make.Text = (myReader["make"].ToString());
                model.Text = (myReader["model"].ToString());
            }
            con1.Close();

ページがセッションであるログインセッションを必要とし、無視できるようにしています。

このコードでは、変更したいフィールドの SQL データベースからデータを取得し、ページの読み込み時にテキスト ボックスに入力します。

ページ読み込みデータが入力された後にテキストボックスを編集して送信をクリックすると、コードは次のようになります

 try
    {

        editUser.ExecuteNonQuery();

        con.Close();
        Response.Redirect("../stockkey1.aspx");


    }
    catch (Exception)
    {
        Response.Write("<br/>Something really bad happened, Please try again<br/>");
    }

    finally
    {
        con.Close();
    }

期待どおりですが、失敗せず、応答を表示しますが、editUser.ExecuteNonQuery(); を見逃します。コマンドを実行し、リダイレクト先に直接リダイレクトします。

私の唯一の考えは、.ToString 変換がこれを引き起こしているということですが、よくわかりません。

何かアイデアがあれば、ぜひ聞きたいです...

ありがとうございました!!

4

2 に答える 2

0

@model where SID ='" + Label1.Text.Trim() + "'"; を試しましたか?

于 2013-07-02T12:45:35.647 に答える
0

ページ読み込みイベントでページがポストバックされているかどうかを確認します。isPostback が false の場合にのみ、テキスト ボックスを読み込みます。そうしないと、ポストバックごとにテキストボックスがリロードされ、更新前に変更が失われます

protected void Page_Load(object sender, EventArgs e)
{ 
if (!Page.IsPostBack)
{
    if (Session["New"] != null)
    {

        Label2.Text = Session["New"].ToString();
        SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["overlandconnectionstring2"].ConnectionString);
        con1.Open();
        SqlDataReader myReader = null;
        SqlCommand myCommand = new SqlCommand("select * from Stock where SID='" + Label1.Text + "'", con1);
        myReader = myCommand.ExecuteReader();

        while (myReader.Read())
        {
            make.Text = (myReader["make"].ToString());
            model.Text = (myReader["model"].ToString());
        }
        con1.Close();
}
}
于 2013-07-02T18:26:01.417 に答える