第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如果出現(xiàn) DBNULL,我如何使消息框出現(xiàn)

如果出現(xiàn) DBNULL,我如何使消息框出現(xiàn)

C#
吃雞游戲 2022-12-31 13:07:55
我有一個(gè)問題,如果我通過 a 搜索數(shù)據(jù)TextBox并且數(shù)據(jù)庫中不存在該數(shù)據(jù),我會(huì)收到錯(cuò)誤消息“對(duì)象不能從 DBNull 轉(zhuǎn)換為其他類型?!蔽以噲D讓自己MessageBox看起來像是在說數(shù)據(jù)不存在并且無法弄清楚如何做到這一點(diǎn)。我嘗試使用一個(gè)if語句,如果TextBox等于DBNull則MessageBox出現(xiàn) a。這不是t work and im not too sure why. The error occurs with me trying to **equal** toDBNull . How do i produce aTextBox` 說數(shù)據(jù)不存在嗎?{        SqlConnection con = new SqlConnection("***COnString**");        con.Open();        SqlCommand comm = new SqlCommand("SELECT SUM (Total_Hours_Day) FROM Sign_In_Out_Table, User_Table WHERE User_Table.FirstName = '" + Search_Username_Alerts_Admin_txtbox.Text + "' AND Sign_In_Out_Table.eb_number = User_Table.eb_number AND Date between GETDATE()-14 and GETDATE()", con);        decimal TotalHoursFortnight = Convert.ToDecimal(comm.ExecuteScalar());        con.Close();        decimal sum = 0;        sum += Convert.ToDecimal(TotalHoursFortnight);        if (Search_Username_Alerts_Admin_txtbox.Text == DBNull)        {            MessageBox.Show("No Data Exists");        }        else        {            MessageBox.Show(Search_Username_Alerts_Admin_txtbox.Text + ":" + Environment.NewLine + " Hours Worked = " + TotalHoursFortnight, ("Working Info Admin"), MessageBoxButtons.OK, MessageBoxIcon.Information);        }}我的預(yù)期結(jié)果是出現(xiàn)一個(gè)消息框,提示搜索時(shí)數(shù)據(jù)不存在。但如果確實(shí)如此,數(shù)據(jù)將顯示在消息框中。
查看完整描述

3 回答

?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

您有3 個(gè)案例需要實(shí)施:

  1. 根本沒有數(shù)據(jù):檢查null

  2. 無效數(shù)據(jù),例如1 + 2 + NULL + 3 == NULL:檢查DBNull.Value

  3. 有效數(shù)據(jù),例如1 + 2 + 3 == 6:借助Convert.ToDecimal()

代碼:

// wrap IDisposable into using

using (SqlConnection con = new SqlConnection("***COnString**")) {

  con.Open();


  //DONE: Make sql readable

  //DONE: Make sql parametrized

  //TODO: you may want to change eb_number = eb_number into INNER JOIN

  string sql = 

    @"SELECT SUM (Total_Hours_Day) 

        FROM Sign_In_Out_Table, 

             User_Table 

       WHERE User_Table.FirstName = @prm_FirstName  

         AND Sign_In_Out_Table.eb_number = User_Table.eb_number 

         AND Date BETWEEN GETDATE() - 14 AND GETDATE()"; 


  using (SqlCommand comm = new SqlCommand(sql, con)) {

    //TODO: Better specify RDBMS type explictly with "comm.Parameters.Add(...)"

    comm.Parameters.AddWithValue(

      "@prm_FirstName", Search_Username_Alerts_Admin_txtbox.Text); 


    var result = comm.ExecuteScalar();


    if (null == result) {              // No Data

      MessageBox.Show("No Data Exist");

    }

    else if (DBNull.Value == result) { // We have the Data and it's RDBMS Null

      MessageBox.Show("Data Exist, but not valid.");

    } 

    else {                             // We have a valid Decimal

      Decimal sum = Convert.ToDecimal(result); 

      //TODO: put the relevant code here

    }

  }

}


查看完整回答
反對(duì) 回復(fù) 2022-12-31
?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

我會(huì)使用 decimal.tryparse 來查看是否返回了數(shù)據(jù)


            SqlConnection con = new SqlConnection("***COnString**");

            con.Open();

            SqlCommand comm = new SqlCommand("SELECT SUM (Total_Hours_Day) FROM Sign_In_Out_Table, User_Table WHERE User_Table.FirstName = '" + Search_Username_Alerts_Admin_txtbox.Text + "' AND Sign_In_Out_Table.eb_number = User_Table.eb_number AND Date between GETDATE()-14 and GETDATE()", con);

            string TotalHoursFortnight = (comm.ExecuteScalar()).ToString();

            con.Close();


            decimal sum = 0;

            decimal temp;

            if(!decimal.TryParse(TotalHoursFortnight, out temp)) 

            { 


                MessageBox.Show("No Data Exists");

            }

            else

            {

                sum += temp;


                MessageBox.Show(Search_Username_Alerts_Admin_txtbox.Text + ":" + Environment.NewLine + " Hours Worked = " + TotalHoursFortnight, ("Working Info Admin"), MessageBoxButtons.OK, MessageBoxIcon.Information);

            }


查看完整回答
反對(duì) 回復(fù) 2022-12-31
?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

您需要為查詢結(jié)果檢查 null


var result = comm.ExecuteScalar();

    if(result != null){

         decimal TotalHoursFortnight = Convert.ToDecimal(comm.ExecuteScalar());

         // move remain code to if block

    }

并且if (Search_Username_Alerts_Admin_txtbox.Text == DBNull)應(yīng)該改為


if (Convert.IsDBNull(Search_Username_Alerts_Admin_txtbox.Text){


}


查看完整回答
反對(duì) 回復(fù) 2022-12-31
  • 3 回答
  • 0 關(guān)注
  • 129 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)