Solved

Embedded statement cannot be a declaration or labeled statement

Posted on 2007-11-20
3
959 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 142

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now