Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#捕获不工作(无效令牌)_C#_Windows_Visual Studio 2013 - Fatal编程技术网

C#捕获不工作(无效令牌)

C#捕获不工作(无效令牌),c#,windows,visual-studio-2013,C#,Windows,Visual Studio 2013,捕获的是无效的令牌。不知道如何修复 对于那些好奇的人,我正在制作一个delete函数来删除我的大学数据库中的某些值。更改您的代码以添加一个try,然后检查异常详细信息以查找任何内部异常 public Boolean Delete(Int32 HolidayNo) { //provides the functionality for the delete class //create an instance of the data conn

捕获的是无效的令牌。不知道如何修复


对于那些好奇的人,我正在制作一个delete函数来删除我的大学数据库中的某些值。

更改您的代码以添加一个try,然后检查异常详细信息以查找任何内部异常

        public Boolean Delete(Int32 HolidayNo)
    {
        //provides the functionality for the delete class

        //create an instance of the data connection class called MyDatabase
        clsDataConnection MyDatabase = new clsDataConnection();
        //add the HolidayNo parameter passed to this function to the list of parameters to use in the database
        MyDatabase.AddParameter("@HolidayNo", HolidayNo);
        //execute the stored procedure in the database
        MyDatabase.Execute("sproc_tblHolidays_Delete");
        //return value for function
        return true;
    }
    catch
    {
        return false;
    }
}

我甚至不知道它是如何编译的。请先清理一下你的方法。像这样:

public Boolean Delete(Int32 HolidayNo)
{
    try
    {
        //provides the functionality for the delete class

        //create an instance of the data connection class called MyDatabase
        clsDataConnection MyDatabase = new clsDataConnection();
        //add the HolidayNo parameter passed to this function to the list of parameters to use in the database
        MyDatabase.AddParameter("@HolidayNo", HolidayNo);
        //execute the stored procedure in the database
        MyDatabase.Execute("sproc_tblHolidays_Delete");
        //return value for function
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

您得到
意外标记
错误的原因是
catch
块完全超出了您的方法。此外,您还缺少
try/catch
中的
try
部分。要更正此问题,请将
catch
块放在方法中,并将其余代码放在
try
块中,就在
catch
之前:

public Boolean Delete(Int32 HolidayNo)
{
    var deleted = false;
    try {
        if (HolidayNo > 0) {
            //provides the functionality for the delete class

            //create an instance of the data connection class called MyDatabase
            clsDataConnection MyDatabase = new clsDataConnection();
            //add the HolidayNo parameter passed to this function to the list of parameters to use in the database
            MyDatabase.AddParameter("@HolidayNo", HolidayNo);
            //execute the stored procedure in the database
            MyDatabase.Execute("sproc_tblHolidays_Delete");

            deleted = true;
        }
    }
    catch (Exception ex)
    {
        // TODO: Log exception ex
        return deleted;
    }
}

try
在哪里?从这里开始这是编译器错误还是运行时错误?听起来和看起来都像编译器…您无法最终将return放入其中。此外,此示例完全更改了接受异常的原始行为,并对原始方法中未指定的输入添加了新的验证…抱歉。是的。试图在没有智能感知的情况下打字。我已经更新了。我为输入添加了验证。我从不在不验证输入的情况下直接调用数据库。。。浪费资源。我不明白这个可变的答案。。我需要向我的导师解释我的代码,但我不能用这个来解释。太棒了,工作得很愉快!谢谢你,鲁弗斯,谢谢你们的帮助
public Boolean Delete(Int32 HolidayNo)
{
    try
    {
        clsDataConnection MyDatabase = new clsDataConnection();
        MyDatabase.AddParameter("@HolidayNo", HolidayNo);
        MyDatabase.Execute("sproc_tblHolidays_Delete");
        return true;
    }    
    catch
    {
        return false;
    }
}