VB.NET TO C#-Gridview代码后面,用于从带有存储过程的列表框中进行搜索 [英] VB.NET TO C# - Gridview Code behind for a search from a listbox w a stored procedure

查看:70
本文介绍了VB.NET TO C#-Gridview代码后面,用于从带有存储过程的列表框中进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#语法中寻找正确的代码,以显示Gridview中多个参数的搜索结果.我只是在VB.NET中做了类似的事情,但是我必须在C#.NET中更新一个项目,并且不确定:

I am looking for the correct code behind syntax in C# for displaying Search Results from multiple parameters in Gridview. I just did something similar in VB.NET but I have to update a project in C#.NET and am unsure of:

1)C#.NET中正确的代码隐藏语法(VB.NET中的代码)

1) the correct codebehind syntax in C#.NET (code in VB.NET)

2)在用户从列表框中选择多个保险计划参数,多个年龄参数和/或M的情况下,如何指定多个搜索参数

2) how to specify multiple search paramaters in the case that a user selects multiple Insurance Plan parameters, Multiple Age parameters, and/or M from a Listbox

3)禁用或启用在列表框中选择多个参数的功能. (对于状态"和邮政编码列表"框,我想禁用选择多个参数,而在计划",年龄"和承运人"上,我希望启用选择多个参数.

3) disable or enable the ability to select multiple parameters in a listbox. (For State and Zip List boxes I'd like to disable selecting multiple parameters and on Plan, Age, and Carrier I'd like to enable selecting multiple parameters.

这就是我在VB.NET后端中需要在C#.NET中使用的内容:

Here's what I have on the backend in VB.NET that needs to be in C#.NET:

      Protected Sub SearchButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SearchButton.Click
        With Me.SqlDataSource1
        .SelectParameters.Clear()

        .ConnectionString = ConfigurationManager.AppSettings("PriceFinderConnectionString").ToString
        .SelectCommand = "up_SelectPriceFinderResults" //calling stored procedure 
        .SelectParameters.Add("state_code", Me.lastname.Text)
        .SelectParameters.Add("zip_code", Me.city.Text)
        .SelectParameters.Add("plan_name", Me.state.Text)
        .SelectParameters.Add("age", Me.state.Text)
        .SelectParameters.Add("carrier_name", Me.donotmail.Text)

        .SelectParameters(0).ConvertEmptyStringToNull = True
        .SelectParameters(1).ConvertEmptyStringToNull = True
        .SelectParameters(2).ConvertEmptyStringToNull = True
        .SelectParameters(3).ConvertEmptyStringToNull = True
        .SelectParameters(4).ConvertEmptyStringToNull = True
        .SelectCommandType = SqlDataSourceCommandType.StoredProcedure

        .CancelSelectOnNullParameter = False

    End With

    GridView2.DataBind()
  End Sub

以下是存储过程的代码:

Here's the code for the stored procedure:

PROCEDURE [dbo].[up_SelectPriceFinderResults] 
-- Add the parameters for the stored procedure here
@state_code varchar(20)= NULL, 
@zip_code varchar(20)= NULL,
     @plan_code varchar(2)= NULL, 
     @insur_age varchar(2)= NULL,
@carrier_name varchar(20)= NULL,

     AS
     BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.

SET NOCOUNT ON;

-- Insert statements for procedure here

IF len(@state_code)=0 
   BEGIN SET @state_code=NULL
   END
IF len(@zip_code)=0
   BEGIN SET @zip_code=NULL 
   END  
IF len(@plan_name)=0
   BEGIN SET @plan_code=NULL
   END
IF len(@insur_age)=0
   BEGIN SET @age=NULL
   END
IF len(@carrier_name)=0
   BEGIN SET @carrier_name=NULL
   END


SELECT  [state_code], 
    [zip_code], 
    [plan_code], 
    [carrier_name],
    [insur_age],  
            [female_value],
    [male_value]        
        CASE WHEN [female_value] is NULL OR 0 
            THEN 'N/A'
        END AS 'female_value',
        CASE WHEN [male_value] is NULL OR 0 
            THEN 'N/A'
        END AS 'male_value',
FROM 
       [state_zipcode_plans] 
WHERE
        (([state_code] = @state_code OR @state_code IS NULL) 
    AND ([zip_code] = @zip_code OR @zip_code IS NULL) 
    AND ([plan_name] = @plan_name OR @plan_name IS NULL) 
    AND ([insur_age] = @insur_age OR @insur_age IS NULL)
    AND ([carrier_name] = @carrier_name OR @carrier_name IS NULL))

ORDER BY 
       [plan_code], [female_value], [male_value]
END

推荐答案

1)有几种方法可以解决此问题:

1) There are several ways to approach this:

在线提供的免费工具

http://www.developerfusion.com/tools/convert/vb -to-csharp/

了解C#语法.

这并不难,即使您主要在VB.net中工作,也能够阅读C#非常方便,因为Web上可用的许多代码示例都在C#中.

It's not hard, and it's extremely handy to be be able to read C# even if you're primarily working in VB.net because many of the code samples available on the web are in C#.

2)使用现有的存储过程方法很难做到这一点.您要么只需要构建动态SQL(使用绑定参数,其中可能),或使用存储的proc构造并执行动态sql.

2) This is difficult to do with the store procedure approach you have. You'll either have to just construct dynamic SQL (using Bind Parameters where possible), or have your stored proc construct and execute the dyanmic sql.

3)这很容易:只需设置

3) This is easy: Just set the SelectionMode property of the ListBoxs accordingly.

这篇关于VB.NET TO C#-Gridview代码后面,用于从带有存储过程的列表框中进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆