C# 如何在asp.net中检查DataRow中的行值是否为空

C# 如何在asp.net中检查DataRow中的行值是否为空,c#,datarow,C#,Datarow,我正在使用OleDbConnection读取Excel值,并将这些值保存在mysql中 在Excel文件中,如果某个单元格值为空,则在添加到数据库时显示错误 所以我想在赋值之前验证每个值。我该怎么做 例如,对于行: colBasic = row[10].ToString(); 如果此行[10]为空,则colBasic应为零 protected void Button1_Click(object sender, EventArgs e) {

我正在使用OleDbConnection读取Excel值,并将这些值保存在mysql中

在Excel文件中,如果某个单元格值为空,则在添加到数据库时显示错误

所以我想在赋值之前验证每个值。我该怎么做

例如,对于行:

colBasic = row[10].ToString(); 
如果此行[10]为空,则colBasic应为零

protected void Button1_Click(object sender, EventArgs e)
{               
            string path = "C:\\Pay.xls";
            string query = "SELECT * FROM [Sheet1$]";
            OleDbConnection conn = new OleDbConnection();

            if (File.Exists(path) == true)
            {
                conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = '" + path + "'" + @";Extended Properties=""Excel 8.0;HDR=NO;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
                conn.Open();

                string ex_id = "";
                string colName = "";
                string colEmpID = "";
                string colBasic = "0";
                string colHRA = "0";
                string colConveyance = "0";

                try
                {
                    OleDbCommand ocmd = new OleDbCommand(query, conn);
                    OleDbDataReader odr = ocmd.ExecuteReader();
                    int nFields = odr.FieldCount;

                    DataTable dtable = new DataTable();
                    dtable.Load(odr);
                    if (dtable.Rows.Count > 0)
                    {
                        DataRow row = dtable.Rows[0];

                        if (GlobalCS.OpenConnection() == true)
                        {                            
                                for (int i = 5; i < dtable.Rows.Count; i++)
                                {
                                    row = dtable.Rows[i];
                                    ex_id = row[0].ToString();
                                    colEmpID = row[2].ToString();
                                    colName = row[1].ToString();
                                    colBasic = row[10].ToString();
                                    colHRA = row[11].ToString();
                                    colConveyance = row[12].ToString();                                    

                                    if (colName != "")
                                    {


                                            sQuery = "insert into salary (EmployeeID,EmployeeName,Basics,DA,HRA,Conveyance,) values(@a,@b,@c,@d,@e,@f)";
                                                                                        cmd = new MySqlCommand(sQuery, GlobalCS.objMyCon);
                                            cmd.Parameters.AddWithValue("a", colEmpID);
                                            cmd.Parameters.AddWithValue("b", colName);
                                            cmd.Parameters.AddWithValue("c", colBasic);
                                            cmd.Parameters.AddWithValue("d", '0');
                                            cmd.Parameters.AddWithValue("e", colHRA);
                                            cmd.Parameters.AddWithValue("f", colConveyance);   
rowsaffected = cmd.ExecuteNonQuery();
 } //close of if(colName!=0)
} //close of for loop

                        }  // close of if(GlobalCS.Openconnection())
                    }  // close of if(dtable.Rows.Count > 0)
                    GlobalCS.CloseConnection();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    string display = ex.Message;
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);

                    GlobalCS.CloseConnection();                    
                    conn.Close();
                }
            }
            else  
            {
                string display = "Payslip.xls file not found";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);

                GlobalCS.CloseConnection();              
                conn.Close();
            }
这应该有用

$(document).ready(function () {
  $(document).keydown(function (event) {
      if (event.ctrlKey == true && (event.which == '107' || event.which == '109' || event.which == '187' || event.which == '189'))
       {
           event.preventDefault();
       }
   });
   });

    protected void Button1_Click(object sender, EventArgs e)
    {               
        string path = "C:\\Pay.xls";
        string query = "SELECT * FROM [Sheet1$]";
        OleDbConnection conn = new OleDbConnection();

        if (File.Exists(path) == true)
        {
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = '" + path + "'" + @";Extended Properties=""Excel 8.0;HDR=NO;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
            conn.Open();

            string ex_id = "";
            string colName = "";
            string colEmpID = "";
            string colBasic = "0";
            string colHRA = "0";
            string colConveyance = "0";

            try
            {
                OleDbCommand ocmd = new OleDbCommand(query, conn);
                OleDbDataReader odr = ocmd.ExecuteReader();
                int nFields = odr.FieldCount;

                DataTable dtable = new DataTable();
                dtable.Load(odr);
                if (dtable.Rows.Count > 0)
                {
                    DataRow row = dtable.Rows[0];

                    if (GlobalCS.OpenConnection() == true)
                    {                            
                            for (int i = 5; i < dtable.Rows.Count; i++)
                            {
                                row = dtable.Rows[i];
                                ex_id = row[0].ToString();
                                colEmpID = row[2].ToString();
                                colName = row[1].ToString();
                                colBasic = !string.IsNullOrEmpty(row[10].ToString()) ? row[10].ToString() : "0";
                                colHRA = row[11].ToString();
                                colConveyance = row[12].ToString();                                    

                                if (colName != "")
                                {


                                        sQuery = "insert into salary (EmployeeID,EmployeeName,Basics,DA,HRA,Conveyance,) values(@a,@b,@c,@d,@e,@f)";
                                                                                    cmd = new MySqlCommand(sQuery, GlobalCS.objMyCon);
                                        cmd.Parameters.AddWithValue("a", colEmpID);
                                        cmd.Parameters.AddWithValue("b", colName);
                                        cmd.Parameters.AddWithValue("c", colBasic);
                                        cmd.Parameters.AddWithValue("d", '0');
                                        cmd.Parameters.AddWithValue("e", colHRA);
                                        cmd.Parameters.AddWithValue("f", colConveyance);   
                                        rowsaffected = cmd.ExecuteNonQuery();
                                } //close of if(colName!=0)
                    } //close of for loop

                    }  // close of if(GlobalCS.Openconnection())
                }  // close of if(dtable.Rows.Count > 0)
                GlobalCS.CloseConnection();
                conn.Close();
            }
            catch (Exception ex)
            {
                string display = ex.Message;
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);

                GlobalCS.CloseConnection();                    
                conn.Close();
            }
        }
        else  
        {
            string display = "Payslip.xls file not found";
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);

            GlobalCS.CloseConnection();              
            conn.Close();
        }

使用:colBasic=行[10]!=无效的第[10]行。ToString:0;不,它仍然是空的。当我在watch窗口中看到它时,[10]行显示为{}.colBasic=!string.IsNullOrEmptyrow[10]?第[10]行。ToString:0;它起作用了。只需将ToString添加到condition.colBasic=!string.IsNullOrEmptyrow[10]。ToString?第[10]行。ToString:0;在isnullorempty调用中不需要toString,这样做将导致null引用异常,尽管每当它为null时。很糟糕的方法,markpsmith的答案在这里更好。无论原始代码多么丑陋;!string.IsNullOrEmptyrow[10]。每当第[10]行为null时,ToString将抛出null引用异常,即使不可能,但看起来不太好,特别是考虑到它不是必需的;
colBasic = !string.IsNullOrEmpty(row[10].ToString()) ? row[10].ToString() : "0";