【问题标题】:Dataset value going in catch if not greater than 0如果不大于 0,则进入 catch 的数据集值
【发布时间】:2018-12-27 10:23:29
【问题描述】:

我在 Dataset 中填充了一些值,但是当我没有返回任何行时,我为那些返回值小于 0 的值做了一个 else 部分。

这里是代码

ds = GetCircleDetails(XY, txtSapIdNHQCircle.Text);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    strState = ds.Tables[0].Rows[0]["R4GSTATENAME"].ToString();

                    if (Save(0))
                    {
                        BindDetails(false);
                    }
                }
                else
                {
                    //ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "checkRdValueForSingle('Please enter valid Lat/Long to submit the details')", true);
                    string latLongMes = "alert('Please enter valid Lat/Long to submit the details')";
                    ScriptManager.RegisterClientScriptBlock((sender as Control), this.GetType(), "alert", latLongMes, true);
                    txtLatNHQCircle.Value = "";
                    txtLongNHQCircle.Value = "";
                    ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "checkRdValueForSingle('')", true);
                    return;
                }
                if (lblStateNHQCircle.Text != strState)
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "checkRdValueForSingle('The entered lat/long is out of the existing State i.e., so could not save the record.');", true);
                    return;
                }                    
            }
            else
            {
                if (Save(0))
                {
                    BindDetails(false);
                }
            }
        }
        else if(Save(0))
        {
            BindDetails(false);
        }            
    }
    catch (Exception ex)
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "alert('Error occured: Could not save change request data.');", true);
        ErrorLog.Save(ex.Message, ex.StackTrace, "ChangeRequestDetails - btnSave_Click", CurrentUserName);
        throw ex;
    }

下面是GetCircleDetails(XY, txtSapIdNHQCircle.Text);的功能

public DataSet GetCircleDetails(string xyCoordinate, string sapid)
{
    string ConnectionStringWFM = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringCR"].ConnectionString;

    DataSet ds = new DataSet();
    OracleConnection oraConn = new OracleConnection(ConnectionStringWFM);

    string strStoreProcedure = "GET_CIRCLENAME_XYPOINT";

    try
    {
        OracleCommand cmd = new OracleCommand(strStoreProcedure, oraConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new OracleParameter("P_XYCORDINATE", xyCoordinate));
        cmd.Parameters.Add(new OracleParameter("P_SAPID", sapid));
        cmd.Parameters.Add(new OracleParameter("OUTR4GSTATENAME", OracleDbType.RefCursor, ParameterDirection.Output));
        cmd.Parameters.Add(new OracleParameter("OUTALLDETAILS", OracleDbType.RefCursor, ParameterDirection.Output));
        if (oraConn.State == ConnectionState.Closed) oraConn.Open();
        OracleDataAdapter da = new OracleDataAdapter(cmd);
        da.Fill(ds);
        return ds;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {

    }        
}

请说明为什么要使用catch 而不是else

更新

我收到错误

ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "alert('Error occured: Could not save change request data.');", true);

但我应该得到它

string latLongMes = "alert('Please enter valid Lat/Long to submit the details')"; ScriptManager.RegisterClientScriptBlock((sender as Control), this.GetType(), "alert", latLongMes, true);

【问题讨论】:

  • 从处理错误开始,而不是抛出一个新错误。它可能会提示您可能出现的错误...
  • @VDWWD:你说的怎么样,请指教
  • 我建议您处理错误并阅读错误消息。那应该告诉您错误是什么,而不是在这里发布所有代码并寻求解决方案。如果您发布错误,帮助您会容易得多。
  • @VDWWD:是的,我正在处理其他部分,对吗??如果不是我的代码有什么问题。 ?
  • @VDWWD:更新问题,看看

标签: c# asp.net datatable dataset


【解决方案1】:

要检查数据集是否为空,您必须检查 null 和表计数。您可以使用 if(ds != null && ds.Tables.Count > 0 )

【讨论】:

  • 那么它会进入其他部分吗??
【解决方案2】:

如果出现错误

ClientScript.RegisterStartupScript

添加此代码

 Responce.Write("<script>alert('Your Text Here');</script>")

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多