0

データベースのテーブルに複数の列があります。特定の列に null 値があるかどうかを確認したい。このコードを使用しましたが、最初のコードでのみ機能し、2 番目のコードでは機能しません。

if (!read.IsDBNull(3))
{
  lblInc3.Visible = true;
  txtInc3.Visible = true;
  lblInc3.Text = read["Income3"].ToString();
}
else if (!read.IsDBNull(4))
{
  lblInc4.Visible = true;
  txtInc4.Visible = true;
  lblInc4.Text = read["Income4"].ToString();
} 
else 
{
  txtInc3.Visible = false;
  txtInc4.Visible = false;
}

複数の列にnull値がない場合にロジックを実行したいだけです。null 値がある場合、テキストボックスは表示されません。

編集:

      try
        {
            SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
            sqlCon.Open();

            SqlCommand com = new SqlCommand("SELECT * FROM Allowance", sqlCon);

            SqlParameter income1 = new SqlParameter("@Income1", SqlDbType.VarChar, 20);
            com.Parameters.Add(income1);
            income1.Direction = ParameterDirection.Output;

            SqlParameter income2 = new SqlParameter("@Income2", SqlDbType.VarChar, 20);
            com.Parameters.Add(income2);
            income2.Direction = ParameterDirection.Output;

            SqlParameter income3 = new SqlParameter("@Income3", SqlDbType.VarChar, 20);
            com.Parameters.Add(income3);
            income3.Direction = ParameterDirection.Output;

            SqlParameter income4 = new SqlParameter("@Income4", SqlDbType.VarChar, 20);
            com.Parameters.Add(income4);
            income4.Direction = ParameterDirection.Output;

            SqlParameter income5 = new SqlParameter("@Income5", SqlDbType.VarChar, 20);
            com.Parameters.Add(income5);
            income5.Direction = ParameterDirection.Output;

            SqlDataReader read = com.ExecuteReader();

            while (read.Read())
            {
                bool threeBool = !read.IsDBNull(1) ? true : false;

                lblInc1.Visible = threeBool;
                txtInc1.Visible = threeBool;
                lblInc1.Text = threeBool ? read["Income1"].ToString() : string.Empty;


                bool fourBool = !read.IsDBNull(4) ? true : false;

                lblInc4.Visible = fourBool;
                txtInc4.Visible = fourBool;
                lblInc4.Text = fourBool ? read["Income4"].ToString() : string.Empty;

                /*if (!read.IsDBNull(1))
                {
                    lblInc1.Visible = true;
                    txtInc1.Visible = true;
                    lblInc1.Text = read["Income1"].ToString();
                }
                else
                {
                    txtInc1.Visible = false;
                }

                if (!read.IsDBNull(2))
                {
                    lblInc2.Visible = true;
                    txtInc2.Visible = true;
                    lblInc2.Text = read["Income2"].ToString();
                }
                else
                {
                    txtInc2.Visible = false;
                }

                if (!read.IsDBNull(3))
                {
                    lblInc3.Visible = true;
                    txtInc3.Visible = true;
                    lblInc3.Text = read["Income3"].ToString();
                }
                else
                {
                    txtInc3.Visible = false;
                }

                if (!read.IsDBNull(4))
                {
                    lblInc4.Visible = true;
                    txtInc4.Visible = true;
                    lblInc4.Text = read["Income4"].ToString();
                }
                else
                {
                    txtInc4.Visible = false;
                }

                if (!read.IsDBNull(5))
                {
                    lblInc5.Visible = true;
                    txtInc5.Visible = true;
                    lblInc5.Text = read["Income5"].ToString();
                }
                else
                {
                    txtInc5.Visible = false;
                }*/
            } catch (Exception ex) {
               throw;
            }
4

3 に答える 3

5

else-ifを使用していて、最初の比較が true と評価された後にステートメントが短絡されているため、2 番目の比較では機能しません。if代わりに秒を実装してください。例えば

if (!read.IsDBNull(3))
{
  lblInc3.Visible = true;
  txtInc3.Visible = true;
  lblInc3.Text = read["Income3"].ToString();
} else
{
   txtInc3.Visible = false;
}

if (!read.IsDBNull(4))
{
  lblInc4.Visible = true;
  txtInc4.Visible = true;
  lblInc4.Text = read["Income4"].ToString();
}  else
{
   txtInc4.Visible = false;
}
于 2013-06-25T12:58:55.440 に答える
1

多分このようなものです、これが役立つことを願っています(私はこれをテストしませんでした:))。

編集済み:あなたのコメントに基づいて、これがあなたの望むものだと思います。

 bool threeBool = !string.IsNullOrEmpty(read["Income3"].ToString());

  lblInc3.Visible = threeBool ;
  txtInc3.Visible = threeBool ;
  lblInc3.Text = threeBool  ? read["Income3"].ToString() : string.empty;


  bool fourBool = !string.IsNullOrEmpty(read["Income4"].ToString());

  lblInc4.Visible = fourBool ;
  txtInc4.Visible = fourBool ;
  lblInc4.Text = fourBool ? read["Income4"].ToString() : string.empty;
于 2013-06-25T13:10:09.827 に答える
0

Else 部分を削除します。最初の部分が True の場合、Else 部分はスキップされます...

if (!read.IsDBNull(3))
{
  lblInc3.Visible = true;
  txtInc3.Visible = true;
  lblInc3.Text = read["Income3"].ToString();
}

if (!read.IsDBNull(4))
{
  lblInc4.Visible = true;
  txtInc4.Visible = true;
  lblInc4.Text = read["Income4"].ToString();
} 
于 2013-06-25T13:00:27.803 に答える