Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Embedded statement cannot be a declaration or labeled statement

Posted on 2007-11-20
3
Medium Priority
?
1,033 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

664 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