GVKun编程网logo

asp.net – InvalidOperationException:没有数据时读取的尝试无效. (SQL)(在没有任何数据时进行无效的读取尝试)

15

在这篇文章中,我们将为您详细介绍asp.net–InvalidOperationException:没有数据时读取的尝试无效.(SQL)的内容,并且讨论关于在没有任何数据时进行无效的读取尝试的相关问题

在这篇文章中,我们将为您详细介绍asp.net – InvalidOperationException:没有数据时读取的尝试无效. (SQL)的内容,并且讨论关于在没有任何数据时进行无效的读取尝试的相关问题。此外,我们还会涉及一些关于.net – XML序列化继承类时的InvalidOperationException、.net – 创建wcf Web服务实例时出现InvalidOperationException、.NET 进程抛出 System.InvalidOperationException、Android’InvocationTargetException’和’UnsupportedOperationException’的知识,以帮助您更全面地了解这个主题。

本文目录一览:

asp.net – InvalidOperationException:没有数据时读取的尝试无效. (SQL)(在没有任何数据时进行无效的读取尝试)

asp.net – InvalidOperationException:没有数据时读取的尝试无效. (SQL)(在没有任何数据时进行无效的读取尝试)

sqlConnection conn = new sqlConnection(AllQuestionspresented.connectionString);

    sqlCommand cmd = new sqlCommand(sb.ToString(),conn);
    cmd.Parameters.Add("@ThreadsID",sqlDbType.Int).Value = commentIDe;
    cmd.Parameters.Add("@CommentsID",sqlDbType.Int).Value = commentIDe;
    try
    {
        conn.open();
        sqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        if (dr != null && dr["Comments"] != null && dr["Name"] != null && dr["Date"] != null && dr["UserID"]!=null)//> Invalid attempt to read when no data is present.
        {
            Comment = dr["Comments"].ToString();
            UserName = dr["Name"].ToString();
            Reputation=Int32.Parse(dr["Reputation"].ToString());
            Time = (DateTime)AllQuestionspresented.TryParse(dr["Date"].ToString());
            UserID = (Guid)dr["UserID"];
        }
        dr.Close();
    }
    finally
    {
        if (conn.State != ConnectionState.Closed)
        {
            conn.Close();
        }
    }

注意:我用while(dr.Read){}来迭代那段代码……这里没有显示.

为什么我会得到那个例外,我该如何摆脱它

更新:

while (reader.Read())//Command runs through all the ThreadIDs that i have!
                {
                    Comments allQ = new Comments((int)reader["CommentsID"]);
                    allComments.Add(allQ);
                }

注释是代码所在的类,它在构造函数中有一个运行我提供的代码的方法.

可能是因为循环运行的次数太多了..然后异常被抛出我是对的吗?

解决方法

您的代码的以下部分是非法的

sqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (dr != null && dr["Comments"] ...)

在访问sqlDataReader的索引器之前,您必须调用一次Read方法(并验证它是否返回true),如文档所述:

The default position of the sqlDataReader is before the first record. Therefore,you must call Read to begin accessing any data.

.net – XML序列化继承类时的InvalidOperationException

.net – XML序列化继承类时的InvalidOperationException

我在将c#类序列化为具有基类的 XML文件时出现问题…这是一个简单的示例:

namespace Domain
{
   [Serializable]
   public class ClassA
   {
      public virtual int MyProperty
      {
         get; set;
      }
   }
}

namespace Derived
{
   public class ClassA : Domain.ClassA
   {
      public override int MyProperty
      {
         get { return 1; } set { /* Do nothing */ }
      }
   }
}

当我尝试序列化Derived.ClassA的实例时,我收到以下异常:

InvalidOperationException(Types’Domain.ClassA’和’Derived.ClassA’都使用XML类型名称’ClassA’,来自命名空间“.使用XML属性为该类型指定唯一的XML名称和/或命名空间.)

问题是我想创建一个简单定义XML文件结构的基类,然后允许其他任何人从该类派生以插入业务规则,但格式化将来自基础.

这是可能的,如果是这样,我如何归属基类来允许这个?

解决方法

如果您可以将派生类重命名为与其基类不同的内容,则可以使用 XmlAttributeOverrides执行此操作:

// For ClassB,which derives from ClassA
XmlAttributes          attributes  = new XmlAttributes();                        
attributes.XmlRoot                 = new XmlRootAttribute("ClassA");

XmlAttributeOverrides  overrides   = new XmlAttributeOverrides();
overrides.Add(typeof(ClassB),attributes);

XmlSerializer   serializer  = new XmlSerializer(typeof(ClassB),overrides);

这将序列化为< ClassA> …< / ClassA>并可以从那序列化为ClassB实例.

据我所知,当基类和派生类具有相同的名称时(除了通过Data Contract Surrogates或其他一些过度杀伤方法完全控制序列化过程之外),没有办法做到这一点.

.net – 创建wcf Web服务实例时出现InvalidOperationException

.net – 创建wcf Web服务实例时出现InvalidOperationException

我有一个从类库引用的WCF Web服务.在项目运行之后,当从类库中创建服务客户端对象时,我收到带有消息的InvalidOperationException:

Could not find default endpoint element that references contract
‘MyServiceReference.IMyService’ in the ServiceModel client
configuration section. This might be because no configuration file was
found for your application,or because no endpoint element matching
this contract Could be found in the client element.

我用来创建实例的代码是:

myServiceClient = new MyServiceClient();

MyServiceClient继承自的地方

System.ServiceModel.ClientBase

我该如何解决这个问题?

注意:我有一个单独的控制台应用程序,它只是创建相同的服务对象并调用它,它没有任何问题.

解决方法

Here is my app.config file of the class library:

您应该将此配置设置放到主应用程序的配置文件中. .NET应用程序(调用您的类库)使用来自它自己的配置文件的数据,而不是来自库配置文件.

.NET 进程抛出 System.InvalidOperationException

.NET 进程抛出 System.InvalidOperationException

如何解决.NET 进程抛出 System.InvalidOperationException?

我已经尝试了我在网上找到的所有解决方案,但都没有奏效。 我确保我已经转义了所有引号并双引号其中有空格的任何路径,我已经确保所有进程正常和正确启动。 但由于某种原因,此进程在内部失败并返回退出代码 100,并抛出此特定错误。

具体的工作:

  • 该函数/进程在 cmd 和 powershell 中运行良好。
  • Windows 事件日志没有说明任何关于错误的额外信息。和标题说的一样。

我尝试过的事情:

  • 所以最初我认为这与用户访问 C 驱动器有关。虽然由于 VS 是在 admin 中运行,我认为这没问题。但是我查看了此链接:https://forum.eset.com/topic/14598-cant-scan-c-drive/ 没有帮助。

  • 然后我发现了一堆相似甚至相同的问题;他们中的很多人导致该过程实际上没有正确启动的解决方案:https://social.msdn.microsoft.com/Forums/vstudio/en-US/e899c6c0-7fba-40ec-a824-1bb26715318f/processstart-invalidoperationexception?forum=csharpgeneral 但是,这并没有提供我的解决方案。

  • 与上述声明类似,我发现了一个旧答案,该答案暗示 processstartinfo 对象的某些属性设置不正确 https://forums.codeguru.com/showthread.php?517237-Process-started-in-dll-works-correctly-in-testing-but-not-in-final-version 但是在更改 processstartinfo 的方式时,这对我也没有任何作用设置的属性。

    • 设置 processstartinfo.WorkingDirectory 和 processstartinfo.FileName,反之亦然。
  • 我还假设 ecls.exe 命令行可执行文件实际上无法像带有属性标签的 cmd 行 exe 一样运行(因为原始命令只是称为“ecls”而不是“ecls.exe”,所以测试了设置 processstartinfo 以启动空白 CMD 控制台然后运行原始扫描 cmd 的方法。但是这最终抛出了相同的错误,所以我知道 ecls.exe 可以以 cmd 行方式运行;它只是中断别的。

  • 我也终于找到了至少给了我一些提示的东西:https://www.experts-exchange.com/questions/29134987/ASP-NET-gets-InvalidOperationException-exception-on-System-Diagnostic-Process-object.html 解决方案是在付费墙后面,但我注意到在这个问题中,他们实际上达到了某种输出,而我的问题甚至从未达到他们收到的输出。所以我知道它在 exe 运行时失败,而不是在 exe 中失败。 使用 StandardOutput 错误日志记录后,我将确切的问题缩小到抛出的这个错误:

    “无法打开 C:\Users\...\Logs\scanner.log: ????????????\r\n”

我不知道自己做错了什么,为此浪费了一天。 请有人给出比windows更好的解释...

// USING ESET
processstartinfo psi = new processstartinfo();
//Set pathway
if (System.IO.File.Exists($@"{ConfigurationManager.AppSettings["ESETPath"]}\ecls.exe"))
    psi.FileName = $@"""{ConfigurationManager.AppSettings["ESETPath"]}\ecls.exe""";
else
    psi.FileName = $@"""{ConfigurationManager.AppSettings["ESETAlternativePath"]}\ecls.exe""";
//Set arguments
psi.Arguments = $"--log-file={ConfigurationManager.AppSettings["ESETLogFilePath"]} --no-log-console --log-all {FilePath}";
psi.CreateNowindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;

//Create process with Process Info
Process proc = Process.Start(psi);
proc.WaitForExit(AllowedTimePeriod);

if (proc.HasExited == false)
{
    proc.Kill();
    throw new Exception("Error occured. Anti-Virus Scan Could not complete the operation. Scanning time period too long.");
}

//Log
string stdout = proc.StandardOutput.ReadToEnd();
string stderr = proc.StandardError.ReadToEnd();

switch (proc.ExitCode)
{
    case 0: VirusFound = false; return true;
    case 1:
        VirusFound = true;
        if (System.IO.File.Exists(FilePath))
        {
            System.IO.File.Delete(FilePath);
        }
        return true;
    default:
        throw new Exception("Error occured. Anti-Virus Scan Could not complete the operation. Returned Error Code " + proc.ExitCode.ToString());
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Android’InvocationTargetException’和’UnsupportedOperationException’

Android’InvocationTargetException’和’UnsupportedOperationException’

我在OPPO X905和OPPO X907上遇到了问题,两者都是 Android 4.0.3(API等级15),我没有使用任何更高级别的API,我在Google和SO上搜索过,都与我的问题不同,下面是我的xml文件和崩溃日志,在此先感谢.
xml文件:(分隔符的边距最初是7.5dp,这可能是问题吗?)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_full"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#eff0f7"
    android:baselineAligned="false"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/item_first_half"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:orientation="vertical" >
        <View
            android:id="@+id/first_path"
            android:layout_width="4dp"
            android:layout_height="4dp"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:background="@drawable/selector_car_action_path" />
        <ImageView
            android:id="@+id/im_car_direction_icon"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:layout_gravity="top"
            android:layout_marginLeft="28dp"
            android:layout_marginStart="28dp"
            android:layout_marginTop="4dp"
            android:background="@drawable/selector_car_action_background"
            android:padding="2dp"
            android:src="@drawable/selector_navi_icon_63" />
        <View
            android:id="@+id/second_path"
            android:layout_width="4dp"
            android:layout_height="4dp"
            android:layout_below="@id/im_car_direction_icon"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:background="@drawable/selector_car_action_path" />
        <TextView
            android:id="@+id/tv_direction"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="15dp"
            android:layout_marginLeft="68dp"
            android:layout_marginRight="15dp"
            android:layout_marginStart="68dp"
            android:singleLine="true"
            android:text=""
            android:textColor="@drawable/selector_car_route_turn_text"
            android:textSize="16sp" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/item_second_half"
        android:layout_width="match_parent"
        android:layout_height="32dp" >
        <View
            android:id="@+id/third_path"
            android:layout_width="4dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:background="@drawable/selector_car_action_path" />
        <TextView
            android:id="@+id/tv_road_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="68dp"
            android:layout_marginStart="68dp"
            android:layout_marginTop="4dp"
            android:singleLine="true"
            android:text=""
            android:textColor="@drawable/selector_car_route_desc"
            android:textSize="16sp" />
        <TextView
            android:id="@+id/divider_for_road_and_distance"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="7dp"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="7dp"
            android:layout_marginStart="7dp"
            android:layout_marginTop="4dp"
            android:layout_toEndOf="@id/tv_road_name"
            android:layout_toRightOf="@id/tv_road_name"
            android:singleLine="true"
            android:text="|"
            android:textColor="@drawable/selector_car_route_desc"
            android:textSize="16sp" />
        <TextView
            android:id="@+id/tv_ride_distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="4dp"
            android:layout_toEndOf="@id/divider_for_road_and_distance"
            android:layout_toRightOf="@id/divider_for_road_and_distance"
            android:singleLine="true"
            android:text=""
            android:textColor="@drawable/selector_car_route_desc"
            android:textSize="16sp" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="15dp"
            android:layout_marginLeft="68dp"
            android:layout_marginRight="15dp"
            android:layout_marginStart="68dp"
            android:background="#dee1f0" />
    </RelativeLayout>
</LinearLayout>

drawable / selector_car_route_desc文件是

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:color="@color/color_999999"/>
    <item android:state_selected="false" android:color="@color/color_333333"/>

</selector>

崩溃日志:

android.view.InflateException: Binary XML file line #106: Error inflating class <unkNown>
android.view.LayoutInflater.createView(LayoutInflater.java:606)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
android.view.LayoutInflater.inflate(LayoutInflater.java:489)
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
android.view.LayoutInflater.inflate(LayoutInflater.java:352)
com.tencent.map.ama.route.ui.view.e.<init>(CarRouteShowItem.java:78)
com.tencent.map.ama.route.ui.view.f.a(CarRouteShowView.java:100)
com.tencent.map.ama.route.ui.view.d.a(CarRouteDetailView.java:37)
com.tencent.map.ama.route.ui.MapStateCarRoute.e(MapStateCarRoute.java:418)
com.tencent.map.ama.route.ui.MapStateCarRoute$1.run(MapStateCarRoute.java:255)
android.os.Handler.handleCallback(Handler.java:605)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4476)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
dalvik.system.NativeStart.main(Native Method)
cause by:
java.lang.reflect.InvocationTargetException: null
java.lang.reflect.Constructor.constructNative(Native Method)
java.lang.reflect.Constructor.newInstance(Constructor.java:417)
android.view.LayoutInflater.createView(LayoutInflater.java:586)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
android.view.LayoutInflater.inflate(LayoutInflater.java:489)
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
android.view.LayoutInflater.inflate(LayoutInflater.java:352)
com.tencent.map.ama.route.ui.view.e.<init>(CarRouteShowItem.java:78)
com.tencent.map.ama.route.ui.view.f.a(CarRouteShowView.java:100)
com.tencent.map.ama.route.ui.view.d.a(CarRouteDetailView.java:37)
com.tencent.map.ama.route.ui.MapStateCarRoute.e(MapStateCarRoute.java:418)
com.tencent.map.ama.route.ui.MapStateCarRoute$1.run(MapStateCarRoute.java:255)
android.os.Handler.handleCallback(Handler.java:605)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4476)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
dalvik.system.NativeStart.main(Native Method)
cause by:
java.lang.UnsupportedOperationException: Can''t convert to dimension: type=0x12
android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
android.view.View.<init>(View.java:2849)
android.widget.TextView.<init>(TextView.java:499)
android.widget.TextView.<init>(TextView.java:492)
java.lang.reflect.Constructor.constructNative(Native Method)
java.lang.reflect.Constructor.newInstance(Constructor.java:417)
android.view.LayoutInflater.createView(LayoutInflater.java:586)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
android.view.LayoutInflater.inflate(LayoutInflater.java:489)
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
android.view.LayoutInflater.inflate(LayoutInflater.java:352)
com.tencent.map.ama.route.ui.view.e.<init>(CarRouteShowItem.java:78)
com.tencent.map.ama.route.ui.view.f.a(CarRouteShowView.java:100)
com.tencent.map.ama.route.ui.view.d.a(CarRouteDetailView.java:37)
com.tencent.map.ama.route.ui.MapStateCarRoute.e(MapStateCarRoute.java:418)
com.tencent.map.ama.route.ui.MapStateCarRoute$1.run(MapStateCarRoute.java:255)
android.os.Handler.handleCallback(Handler.java:605)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4476)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
dalvik.system.NativeStart.main(Native Method)

CarRouteShowItem.java:78的代码是这样的:

View mView = LayoutInflater.from(mContext).inflate(R.layout.car_route_detail_item,null);

CarRouteShowItem不是View的子代,其代码如下:

public class CarRouteShowItem {

    private Context mContext;

    private View mView;

    public CarRouteShowItem(Context context) {
        mContext = context;

        // mView is used by caller 
        mView = LayoutInflater.from(mContext).inflate(R.layout.car_route_detail_item,null);

        // some view finds 
    }

        // some other methods related with specific logic
}

图形布局可以在Eclipse中从API级别8预览到API级别23,但是当我在API级别15的Android Studio中预览时,它会显示错误消息:无法解析资源@style / Widget.TextView,其他API级别没问题,但我没有使用TextView的样式.

解决方法

你需要的只是一个额外的眼睛:-)

enter image description here

android:layout_height="1px"

希望你现在能看到.第二个RelativeLayout中的最后一个子项,其id为item_second_half,类型为< View,将其更改为

android:layout_height="1dp"

让我尽快回顾一下

今天的关于asp.net – InvalidOperationException:没有数据时读取的尝试无效. (SQL)在没有任何数据时进行无效的读取尝试的分享已经结束,谢谢您的关注,如果想了解更多关于.net – XML序列化继承类时的InvalidOperationException、.net – 创建wcf Web服务实例时出现InvalidOperationException、.NET 进程抛出 System.InvalidOperationException、Android’InvocationTargetException’和’UnsupportedOperationException’的相关知识,请在本站进行查询。

本文标签: