WPF错误:运算符“ <=”不能应用于类型为“对象”和“对象”的操作数

时间:2018-12-17 19:26:01

标签: c# wpf vb.net

我正在尝试将vb.net应用程序转换为C#,但是当我转换代码以检查日期单元格是否在两个devexpress dateedits之间时,它将运行下一个代码,但是不幸的是,我收到一条错误消息,指出说:

  

运算符'<='不能应用于类型为'object'和'object'的操作数

我在互联网上进行搜索,但找不到解决问题的方法

这是我的C#代码:

foreach (DataRowView dr in Logs_Grid.Items)
                        {
                            SqlCommand cmd2 = new SqlCommand();
                            SqlDataReader rd2;
                            DataSet ds2 = new DataSet();
                            // Dim CustomerIDfromtheTbl

                            ds2.Clear();
                            cmd2.Connection = conn;
                            // SELECT TOP 1 WITH TIES * FROM International ORDER BY News_ID DESC
                            cmd2.CommandText = "SELECT *  FROM att_download where Machine_IP = '" + IP_txt.Text + "' and User_ID = '" + dr[0] + "' and Verify_Date = '" + Format(Convert.ToDateTime(dr[1]), ("yyyy-MM-dd")) + "' and Verify_Time = '" + Format(Convert.ToDateTime(dr[2]), "HH:mm:ss") + "'   ";
                            rd2 = cmd2.ExecuteReader();
                            // SELECT ID FROM INVOICE_HEAD ORDER BY ID DESC LIMIT 1
                            if (rd2.HasRows)
                            {
                                rd2.Read();
                                // MessageBox.Show("موجود مسبقا" + " " + rd2.Item("User_ID").ToString)
                                rd2.Close();
                            }

                            else if (dr[1] >= From_Date.EditValue && dr[1] <= To_Date.EditValue)
                            {
                                SqlCommand cmd4 = new SqlCommand();
                                SqlDataReader rd4;
                                DataSet ds4 = new DataSet();
                                // Dim CustomerIDfromtheTbl

                                ds4.Clear();
                                cmd4.Connection = conn;
                                // SELECT TOP 1 WITH TIES * FROM International ORDER BY News_ID DESC
                                cmd4.CommandText = "select * from Att_Users  where user_code ='" + dr[0] + "' and  machine_ip ='" + IP_txt.Text + "'";
                                rd4 = cmd4.ExecuteReader();
                                // SELECT ID FROM INVOICE_HEAD ORDER BY ID DESC LIMIT 1
                                if (rd4.HasRows)
                                {
                                    rd4.Read();


                                    string sqlString = "insert into att_download (User_ID,Verify_Date,Verify_Time,Verify_Type,Verify_State,WorkCode,CREATE_USER,CREATE_DATE,CREATE_TIME,User_Name,Emp_Code,Machine_IP) "
                                                                      + "values (@User_ID,@Verify_Date,@Verify_Time,@Verify_Type,@Verify_State,@WorkCode,@CREATE_USER,@CREATE_DATE,@CREATE_TIME,@User_Name,@Emp_Code,@Machine_IP)";
                                    using (SqlCommand cmd = new SqlCommand(sqlString, conn))
                                    {
                                        DateTime now = DateTime.Now;

                                        cmd.Parameters.Add(new SqlParameter("@User_ID", dr[0]));
                                        cmd.Parameters.Add(new SqlParameter("@Verify_Date", dr[1]));
                                        cmd.Parameters.Add(new SqlParameter("@Verify_Time", dr[2]));
                                        cmd.Parameters.Add(new SqlParameter("@Verify_Type", dr[3]));
                                        cmd.Parameters.Add(new SqlParameter("@Verify_State", dr[4]));
                                        cmd.Parameters.Add(new SqlParameter("@WorkCode", dr[5]));
                                        cmd.Parameters.Add(new SqlParameter("@CREATE_USER", Main_UserName));
                                        cmd.Parameters.Add(new SqlParameter("@CREATE_DATE", DateTime.Today.ToString("yyyy-MM-dd")));
                                        cmd.Parameters.Add(new SqlParameter("@CREATE_TIME", now.ToString("HH:mm:ss")));
                                        cmd.Parameters.Add(new SqlParameter("@User_Name", rd4["user_name"]));
                                        cmd.Parameters.Add(new SqlParameter("@Emp_Code", rd4["Emp_Code"]));
                                        cmd.Parameters.Add(new SqlParameter("@Machine_IP", IP_txt.Text));



                                        cmd.ExecuteNonQuery();
                                    }

                                    rd4.Close();
                                }
                            }

这是vb.net代码:

For Each dr As DataRowView In Logs_Grid.Items



                        Dim cmd2 As New SqlCommand
                        Dim rd2 As SqlDataReader
                        Dim ds2 As New DataSet
                        'Dim CustomerIDfromtheTbl

                        ds2.Clear()
                        cmd2.Connection = conn
                        'SELECT TOP 1 WITH TIES * FROM International ORDER BY News_ID DESC
                        cmd2.CommandText = "SELECT *  FROM att_download where Machine_IP = '" & IP_txt.Text & "' and User_ID = '" & dr(0) & "' and Verify_Date = '" & Format(Convert.ToDateTime(dr(1)), ("yyyy-MM-dd")) & "' and Verify_Time = '" & Format(Convert.ToDateTime(dr(2)), "HH:mm:ss") & "'   "
                        rd2 = cmd2.ExecuteReader
                        'SELECT ID FROM INVOICE_HEAD ORDER BY ID DESC LIMIT 1
                        If rd2.HasRows Then

                            rd2.Read()
                            'MessageBox.Show("موجود مسبقا" + " " + rd2.Item("User_ID").ToString)
                            rd2.Close()
                        Else
                            If dr(1) >= From_Date.EditValue And dr(1) <= To_Date.EditValue Then

                                Dim cmd4 As New SqlCommand
                                Dim rd4 As SqlDataReader
                                Dim ds4 As New DataSet
                                'Dim CustomerIDfromtheTbl

                                ds4.Clear()
                                cmd4.Connection = conn
                                'SELECT TOP 1 WITH TIES * FROM International ORDER BY News_ID DESC
                                cmd4.CommandText = "select * from Att_Users  where user_code ='" & dr(0) & "' and  machine_ip ='" & IP_txt.Text & "'"
                                rd4 = cmd4.ExecuteReader
                                'SELECT ID FROM INVOICE_HEAD ORDER BY ID DESC LIMIT 1
                                If rd4.HasRows Then

                                    rd4.Read()


                                    Dim sqlString As String = "insert into att_download (User_ID,Verify_Date,Verify_Time,Verify_Type,Verify_State,WorkCode,CREATE_USER,CREATE_DATE,CREATE_TIME,User_Name,Emp_Code,Machine_IP) " _
                                                                      & "values (@User_ID,@Verify_Date,@Verify_Time,@Verify_Type,@Verify_State,@WorkCode,@CREATE_USER,@CREATE_DATE,@CREATE_TIME,@User_Name,@Emp_Code,@Machine_IP)"
                                    Using cmd As SqlCommand = New SqlCommand(sqlString, conn)

                                        cmd.Parameters.Add(New SqlParameter("@User_ID", dr(0)))
                                        cmd.Parameters.Add(New SqlParameter("@Verify_Date", dr(1)))
                                        cmd.Parameters.Add(New SqlParameter("@Verify_Time", dr(2)))
                                        cmd.Parameters.Add(New SqlParameter("@Verify_Type", dr(3)))
                                        cmd.Parameters.Add(New SqlParameter("@Verify_State", dr(4)))
                                        cmd.Parameters.Add(New SqlParameter("@WorkCode", dr(5)))
                                        cmd.Parameters.Add(New SqlParameter("@CREATE_USER", Main_UserName))
                                        cmd.Parameters.Add(New SqlParameter("@CREATE_DATE", Format(Date.Today, ("yyyy-MM-dd"))))
                                        cmd.Parameters.Add(New SqlParameter("@CREATE_TIME", Format(TimeOfDay, "hh:mm:ss")))
                                        cmd.Parameters.Add(New SqlParameter("@User_Name", rd4.Item("user_name").ToString))
                                        cmd.Parameters.Add(New SqlParameter("@Emp_Code", rd4.Item("Emp_Code").ToString))
                                        cmd.Parameters.Add(New SqlParameter("@Machine_IP", IP_txt.Text))



                                        cmd.ExecuteNonQuery()
                                    End Using

                                    rd4.Close()
                                End If


                            End If
                        End If


                    Next

此行生成错误:

 else if (dr[1] >= From_Date.EditValue && dr[1] <= To_Date.EditValue)

XAML代码是:

 <DataGrid x:Name="Logs_Grid" IsReadOnly="True" SelectionUnit="FullRow" AutoGenerateColumns="False" CanUserAddRows="True"  Margin="138,179,10,10">
            <DataGrid.Resources>
                <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Background" Value="#F78A09" />
                    <Setter Property="Foreground" Value="#F0F0F0" />


                </Style>


                <Style TargetType="ScrollBar" >
                    <Setter Property="Background" Value="#FAFAFA" />

                </Style>



                <Style TargetType="DataGridCell">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#360E05"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>

            </DataGrid.Resources>

            <DataGrid.Columns>
                <DataGridTextColumn  Header="كوده بالجهاز" x:Name="UserID" IsReadOnly="True" Binding="{Binding Path=UserID}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="تاريخ البصمة" x:Name="VerifyDate" IsReadOnly="True" Binding="{Binding Path=VerifyDate}" Foreground="#F0F0F0" />
                <DataGridTextColumn Header="توقيت البصمة" x:Name="VerifyTime" IsReadOnly="True" Binding="{Binding Path=VerifyTime}" Foreground="#F0F0F0"/>

                <DataGridTextColumn Header="نوع البصمة" x:Name="VerifyType" IsReadOnly="True" Binding="{Binding Path=VerifyType}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="حالة البصمة" x:Name="VerifyState" IsReadOnly="True" Binding="{Binding Path=VerifyState}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="كود العمل" x:Name="WorkCode" IsReadOnly="True" Binding="{Binding Path=WorkCode}" Foreground="#F0F0F0"/>

            </DataGrid.Columns>



            <DataGrid.RowStyle>
                <Style  TargetType="DataGridRow" >


                    <Setter Property="Background" Value="#8CB3EF"></Setter>

                </Style>
            </DataGrid.RowStyle>

        </DataGrid>


        <dxe:DateEdit x:Name="From_Date" HorizontalAlignment="Left" Margin="10,251,0,0" VerticalAlignment="Top" Width="120"/>
        <dxe:DateEdit x:Name="To_Date" HorizontalAlignment="Left" Margin="10,314,0,0" VerticalAlignment="Top" Width="120"/>

1 个答案:

答案 0 :(得分:1)

如果在VB中打开Option Strict,则会出现相同的错误。当Option Strict为Off时,VB尝试为您转换变量,而C#则不这样做。您需要将变量转换为适当的数据类型。像这样:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
    <form action="formulario.php" method="post">
        <input name="name" placeholder="Nombre"/>
        <input name="email" placeholder="Email"/>
        <div class="g-recaptcha" data-sitekey="MY-API-KEY"></div>
        <button type="submit" name="submit">Enviar</button>
    </form>
    </body>
</html>

<?php
    $name = stripslashes($_POST["name"]);
    $email = stripslashes($_POST["email"]);

    $recaptcha = $_POST["g-recaptcha-response"];

    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => 'MY-SECRET-KEY',
        'response' => $recaptcha,
        ‘remoteip’ => $_SERVER[‘HTTP_CLIENT_IP’]
    );
    $options = array(
        'http' => array (
            'method' => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $verify = file_get_contents($url, false, $context);
    $captcha_success = json_decode($verify);
    if ($captcha_success->success) {
        // No eres un robot, continuamos con el envío del email
        // ...
        // ...
        echo "<h2>No eres un robot</h2>";
    } else {
        // Eres un robot!
        echo "<h2>Eres un robot!</h2>";
    }
?>

或使用该方法获取适当的变量

else if ((DateTime)dr[1] >= From_Date.EditValue && (DateTime)dr[1] <= To_Date.EditValue)