Solved

Embedded statement cannot be a declaration or labeled statement

Posted on 2007-11-20
3
986 Views
Last Modified: 2010-04-21
I am trying to build a if statement around a search variable for sql.  Basically, I want to make the variable null if the user does not key in any search criteria, if they do then I assign it to the scalar variable.  When I try to to set the variable to null I get a the above error message.  Here is my code where it occurs.  Any ideas would be greatly appreciated.

Thanks
Kenny


protected SqlDataAdapter CreateProviderInsAdapter()
    {
        SqlConnection connProvIns = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand(SearchProviderIns, connProvIns);
       

        if
            (txtSearchInsur.Text != "")
            cmd.Parameters.Add(new SqlParameter("@SearchInsur", txtSearchInsur.Text));
        else
            string @SearchInsur = "";    <<<<<--------------<<<<<  Error occurs here



        SqlDataAdapter sqlDAphyins = new SqlDataAdapter(cmd);
        sqlDAphyins.FillSchema(HDSDataSet, SchemaType.Mapped, "PHYSINS");
        sqlDAphyins.Fill(HDSDataSet, "PHYSINS");
        return sqlDAphyins;
    }

0
Comment
Question by:kwh3856
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20323440
>string @SearchInsur = "";    
what is that statement to be doing, actually?


I assume you want to do this:

if (txtSearchInsur.Text != "")
   cmd.Parameters.Add(new SqlParameter("@SearchInsur", txtSearchInsur.Text));
else
   cmd.Parameters.Add(new SqlParameter("@SearchInsur", string.Empty));

0
 

Author Closing Comment

by:kwh3856
ID: 31410228
Your AWEOME!!!!!!

Thanks...once again!!
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20323485
in which case, you don't need the if() else at all?...

just that statement will do fine:
cmd.Parameters.Add(new SqlParameter("@SearchInsur", txtSearchInsur.Text));
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question