Solved

Embedded statement cannot be a declaration or labeled statement

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

15 Experts available now in Live!

Get 1:1 Help Now