VB.NET TO C# - Gridview Code behind for a search from a listbox w a stored procedure
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) the correct codebehind syntax in C#.NET (code in VB.NET)
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) 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.
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 = T开发者_如何学Crue
.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) 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();
}
精彩评论