-4

2 つのテキスト ボックスと 2 つの送信ボタンがあります。各ボタンはテキストボックス内の情報を送信します。両方のテキストボックスを一度に送信する送信ボタンが 1 つ必要です。問題は、1 つのテキスト ボックスが空の場合、データが空のデータで上書きされることです。テキストボックスに何かがある場合にのみ情報を送信したいと思います。いくつかの疑似コード コードが役立つ場合があります。

  • textbox1 が空の場合は何もせず、textbox2 にデータがある場合はデータベースを更新します。
  • textbox1 にデータがある場合は更新し、textbox2 が空の場合は textbox2 を更新しません。

これが私のコードです。私が言っていることが理にかなっていることを願っています。

protected void Button3_Click(object sender, EventArgs e)
    {

            SqlConnection conns = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDBConnectionString1"].ConnectionString);
            SqlCommand cmd = new SqlCommand("UPDATE test SET SitUps = @SitUps WHERE (Id = @Id)", conns);
            cmd.CommandType = CommandType.Text;
            Label IdL = ((Label)FormView1.FindControl("IdLabel"));
            cmd.Parameters.AddWithValue("@Id", IdL.Text);
            cmd.Parameters.AddWithValue("@SitUps", Sits.Text);
            conns.Open();
            cmd.ExecuteNonQuery();
            Label17.Text = "Successfully Submitted Sit-Ups!";

    }

    protected void Button4_Click(object sender, EventArgs e)
    {

            SqlConnection conns = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDBConnectionString1"].ConnectionString);
            SqlCommand cmd = new SqlCommand("UPDATE test SET pushUps = @pushUps WHERE (Id = @Id)", conns);
            cmd.CommandType = CommandType.Text;
            Label IdL = ((Label)FormView1.FindControl("IdLabel"));
            cmd.Parameters.AddWithValue("@Id", IdL.Text);
            cmd.Parameters.AddWithValue("@pushUps", Push.Text);
            conns.Open();
            cmd.ExecuteNonQuery();
            Label18.Text = "Successfully Submitted Push-Ups!";

    }
4

2 に答える 2

2

これを次のように置き換えます。

protected void Button_Click(object sender, EventArgs e)
    {
//check Sits and Push empty or not
if(Sits.Text!="") 
{
//update Sits here
}

if(Push.Text!="") 
 {
//update Push here
 }
    }
于 2012-09-05T22:00:05.240 に答える
2

実行しようとしている各ブロックの前に、 if(Push.Text!="") と if(Sits.Text!="") を追加するだけでよいようです

于 2012-09-05T21:42:55.940 に答える