VB.NET à C # - Code GridView Derrière pour une recherche dans une liste de la liste avec une procédure stockée

StackOverflow https://stackoverflow.com/questions/5031458

Question

Je cherche le code correct de la syntaxe en C # pour afficher les résultats de la recherche de plusieurs paramètres dans GridView.Je viens de faire quelque chose de similaire dans vb.net, mais je dois mettre à jour un projet en C # .NET et je suis incertain de:

1) la syntaxe de codeBeHind correcte dans c # .NET (code dans vb.net)

2) Comment spécifier plusieurs paramètres de recherche dans le cas où un utilisateur sélectionne plusieurs paramètres de plan d'assurance, plusieurs paramètres d'âge et / ou m à partir d'une liste

3) Désactivez ou activez la possibilité de sélectionner plusieurs paramètres dans une liste de liste.(Pour les cases de liste d'état et zip, je voudrais désactiver la sélection de plusieurs paramètres et sur le plan, l'âge et le transporteur, je voudrais activer la sélection de plusieurs paramètres.

Voici ce que j'ai sur le backend in vb.net qui doit être en 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

Voici le code de la procédure stockée:

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

Était-ce utile?

La solution

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.

Autres conseils

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();
 }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top