Solved

Embedded statement cannot be a declaration or labeled statement

Posted on 2007-11-20
3
1,002 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

710 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