VB.NETからC# - リストボックスから検索のためのグリッドビューコードW A
-
14-11-2019 - |
質問
GridViewの複数のパラメータから検索結果を表示するためのC#の構文の背後にある正しいコードを探しています。私はただvb.netで似たようなものをしましたが、私はC#.NETでプロジェクトを更新し、わからない:
1)C#.NET(vb.netのコード)で正しいコードベーインド構文。
2)ユーザが複数の保険プランパラメータ、複数の年齢パラメータ、および/またはmから複数の検索パラメータを指定する方法
3)リストボックス内の複数のパラメータを選択する機能を無効または有効にします。(状態とZIPのリストボックスの場合は、複数のパラメータの選択と計画、年齢、およびキャリアの選択を無効にしたいと思います。
これは、vb.netのバックエンドで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
.
ストアドプロシージャのコード:
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) There are several ways to approach this:
Free tools available on line:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
Learn the C# syntax.
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) 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) This is easy: Just set the SelectionMode property of the ListBoxs accordingly.
他のヒント
Using the converter tool, here's hte code I came up w/ in C#:
protected void SearchButton_Click(object sender, EventArgs e)
{
var _with1 = this.SqlDataSource1;
_with1.SelectParameters.Clear();
_with1.ConnectionString = ConfigurationManager.AppSettings("AgentLeadsConnectionString").ToString;
_with1.SelectCommand = "up_SelectMktDataLeads";
_with1.SelectParameters.Add("state_code", this.state_code.Text);
_with1.SelectParameters.Add("zip_code", this.zip_code.Text);
_with1.SelectParameters.Add("plan_name", this.plan_name.Text);
_with1.SelectParameters.Add("age", this.age.Text);
_with1.SelectParameters.Add("carrier_name", this.carrier_name.Text);
_with1.SelectParameters(0).ConvertEmptyStringToNull = true;
_with1.SelectParameters(1).ConvertEmptyStringToNull = true;
_with1.SelectParameters(2).ConvertEmptyStringToNull = true;
_with1.SelectParameters(3).ConvertEmptyStringToNull = true;
_with1.SelectParameters(4).ConvertEmptyStringToNull = true;
_with1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
_with1.CancelSelectOnNullParameter = false;
GridView2.DataBind();
}