如果bot critera为null或'',SQL查询将不会显示所有记录

时间:2017-01-22 19:50:06

标签: sql asp.net sql-server

我使用C#和SQL Server 2014创建了一个ASP.NET网页。

我的GridView1 SqlDataSource SELECT在网页控件中有2个条件Decimal(18,0)查询。

两者都是具有WHERE值的DropDownLists。

我想设置一个if (!Page.IsPostBack) { GridView1.DataBind(); } 子句,以便gridview返回:

  • 如果两个条件均为空或
  • ,则为所有记录
  • 如果满足其中一个条件,则仅使用此条件显示记录,其他条件为null或
  • 如果满足两个条件,则使用两个条件显示记录

某种级联过滤。

我试过这个(在页面加载时):

DropDownList1

DropDownList2 AppendDataBoundItems="True" and <asp:ListItem Text="" Value="" /> 都已设置:

GridView1

因此,DDL控件在页面加载时不包含任何值或文本。

在这种情况下,我希望SELECT.... WHERE ([rentnr] = @rentnr) AND ([apartman] = @apartman) OR ([apartman]= @apartman) AND (@rentnr IS NULL) OR ([rentnr] = @rentrn) AND (@apartman IS NULL) OR (@rentnr IS NULL) AND (@apartman IS NULL) 向我显示所有记录 - 但它没有显示任何内容。

SQL查询语句是:

@rent
  • @apartman是来自DDL1
  • 的数据
  • private void SaveAlarms() { Log.d("NewDay", "Saving Alarms"); new Thread(new Runnable() { @Override public void run() { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); String temp = gson.toJson(_Alarms.toArray(), NDAlarm[].class); _Editor.putString("Alarms",temp); _Editor.commit(); Log.d("NewDay", "Saved Alarms"); } }).start(); } 是来自DDL2的数据

我做错了什么?

感谢您的帮助,谢谢。

3 个答案:

答案 0 :(得分:1)

这个怎么样:

SELECT....
WHERE 
    ( ([rentnr] = @rentnr) AND ([apartman] = @apartman) )
    OR ( ([apartman]= @apartman) AND (@rentnr IS NULL) )
    OR ( ([rentnr] = @rentrn) AND (@apartman IS NULL) )
    OR ( (@rentnr IS NULL) AND (@apartman IS NULL) )

注意,我已经为条件组添加了额外的()。

@rentnr和@apartman在未填写时可能不会作为NULL而是作为0传递给SQL,因为您说DDL将它们列为Decimals。那么你还需要一个OR条件:

    OR ( (@rentnr = 0) AND (@apartman = 0) )

HTH

答案 1 :(得分:1)

最后我找到了解决方案here。 关键是 CancelSelectOnNullParameter =&#34; false&#34; !我把它添加到asp:SqlDataSource作为额外的功能和宾果游戏!

谢谢大家!

答案 2 :(得分:0)

我认为这是你想要的逻辑:

class ReservationsController < ApplicationController
    before_action :authenticate_user!, except: [:notify]

    skip_before_filter :verify_authenticity_token

    def preload
        training = Training.find(params[:training_id])
        today = Date.today
        reservations = training.reservations.where("date >= ?", today)

        render json: reservations
    end

    def create



        @thrill = Thrill.find(params[:thrill_id])
        if @thrill.reservations.length < @thrill.training.tr_max_attendants
            @reservation = current_user.reservations.create(reservation_params)

            if @reservation

                require 'Mollie/API/Client'

                mollie = Mollie::API::Client.new('test_gUejkz43UkdeCauC22J6UNqqVRdpwW')

                payment = mollie.payments.create(
                    amount: 10.00,
                    description: 'My first API payment',
                    redirect_Url: 'http://d7459af1.ngrok.io/your_trips',
                    webhookUrl: 'http://d7459af1.ngrok.io/notify',
                    metadata: {
                        reservationid: @reservation.id
                    }
                )



                @reservation.update_attributes payid:payment.id

                redirect_to payment.payment_url


            else
                redirect_to @thrill.training, notice: "Helaas, de training is vol"
            end
        end
    end

    protect_from_forgery except: [:notify]
#   protect_from_forgery with: :null_session, if: -> {request.format.json?}

    def notify

        require 'Mollie/API/Client'

        mollie = Mollie::API::Client.new('test_gUejkz43UkdeCauC22J6UNqqVRdpwW')

        payment = mollie.payments.get(payment.id)

        params.permit!
        status = params[:payment_status]

        if payment.paid?

          reservation = Reservation.find(params[:payid])
          reservation.update_attributes status:true
        else
            reservation.destroy
        end

        render nothing: true

    end

    protect_from_forgery except: [:your_trips]
    def your_trips
        @reservations = current_user.reservations.joins(:thrill).order('thrills.thrilldate asc').where('? <= thrills.thrilldate', Date.today)
    end

    def your_reservations
        @trainings = current_user.trainings
    end

    private
        def reservation_params

            params.permit(:thrill_id, :user_id)
        end


    end