Solved

Must declare the scalar variable "@SearchCity"

Posted on 2007-11-20
10
614 Views
Last Modified: 2010-04-21
I am attempting to fill the schema in a data set and keep getting a "Must declare the scalar variable "@SearchCity".  The problem is that I think I have already defined the variable.  Can anyone see what I am missing.  Here is my code.

Thanks
Kenny


  protected SqlDataAdapter CreateProviderAdapter()
    {
        SqlConnection conn = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand(ProviderSearch, conn);

        if
            (txtSearchCity.Text != "")
            cmd.Parameters.Add(new SqlParameter("@SearchCity", txtSearchCity.Text));
        else
            cmd.Parameters.Add(new SqlParameter("@SearchCity", string.Empty));
               
        if
            (txtSearchState.Text != "")
            cmd.Parameters.Add(new SqlParameter("@SearchState", txtSearchState.Text));
        else
            cmd.Parameters.Add(new SqlParameter("@SearchState", string.Empty));

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

       
        SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
        sqlDA.FillSchema(HDSDataSet, SchemaType.Mapped, "PROVIDER");
        sqlDA.Fill(HDSDataSet, "PROVIDER");

        return sqlDA;
    }

    private String ConnString
    {
        get
        {
            //HDS ConnectionString comes from the Web.Config
            return ConfigurationManager.ConnectionStrings["Chart RelayConnectionString"].ConnectionString;

        }
    }

    private String ProviderSearch
    {
        get
            {
                 String srchCmd = "Select PHYSICIAN_NPI, LAST_NAME, FIRST_NAME, ADDRESS_1, CITY, STATE, PHONE from Provider";

                 string whereClause = string.Empty;

                 if (txtSearchCity.Text.Length > 0)
                    whereClause = " WHERE CITY = @SearchCity";
                 if (txtSearchState.Text.Length > 0)
                      {
                         if (whereClause.Length > 0)
                              whereClause += " AND ";

                         else
                             whereClause = " WHERE ";
                             whereClause += "STATE = @SearchState";
                      }
                 if (txtSearchZip.Text != "")
                      {
                         if (whereClause.Length > 0)
                             whereClause += " AND ";

                         else
                             whereClause = " WHERE ";
                             whereClause += "ZIP = @SearchZip";
                      }
                      return srchCmd + whereClause + " ORDER BY WEIGHT";
            }
    }



    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
            cmd.Parameters.Add(new SqlParameter("@SearchInsur", string.Empty));

        SqlDataAdapter sqlDAphyins = new SqlDataAdapter(cmd);
        sqlDAphyins.FillSchema(HDSDataSet, SchemaType.Mapped, "PHYSINS");
        sqlDAphyins.Fill(HDSDataSet, "PHYSINS");-------------------------<<<<<<<<<<<<<<<<<<<<-------------ERROR Occurs Here
        return sqlDAphyins;
    }

    protected SqlDataAdapter CreateProviderSpecialAdapter()
    {
        SqlConnection connPhysSpecial = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand(SearchProvSpecialty, connPhysSpecial);

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

        SqlDataAdapter sqlDAphyspecial = new SqlDataAdapter(cmd);
        sqlDAphyspecial.FillSchema(HDSDataSet, SchemaType.Mapped, "PHYS_SPECIALTIES");
        sqlDAphyspecial.Fill(HDSDataSet, "PHYS_SPECIALTIES");

        return sqlDAphyspecial;
    }


0
Comment
Question by:kwh3856
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20323636
as you do this:
>                 if (txtSearchCity.Text.Length > 0)
>                    whereClause = " WHERE CITY = @SearchCity";

you don't need the "else" part in here:

  if (txtSearchCity.Text != "")
            cmd.Parameters.Add(new SqlParameter("@SearchCity", txtSearchCity.Text));
//        else
//            cmd.Parameters.Add(new SqlParameter("@SearchCity", string.Empty));
               


0
 

Author Comment

by:kwh3856
ID: 20323683
I commented those lines and still get the error message.  Is there something else I am missing?

Thanks
Kenny
0
 
LVL 18

Accepted Solution

by:
vbturbo earned 500 total points
ID: 20323716
perhaps

declare your cmd as cmd1

SqlCommand cmd1 = new SqlCommand(SearchProviderIns, connProvIns);
SqlDataAdapter sqlDAphyins = new SqlDataAdapter(cmd1);

as you have a cmd variable declared once
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20323744
though it should be a new instance of the command object and also should outside scope of the first declaration
but according the err msg it looks like it use's the first cmd object in the second fill

vbturbo
0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20323928
Have you tried clearing the DataSet before each Fill?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:townsma
ID: 20324862
I may be going blind in my old age, but there is no definition shown above for the "SearchProviderIns", can you please show this.  I suggect this is probably needing a parameter called @SearchCity, but you are not providing it.  
0
 

Author Comment

by:kwh3856
ID: 20325253
Here is all the code.  I did not realize it was not pasted in.

Thanks
Kenny






--------------------------------------------------------------------------------------------------------------------------------------

private DataSet mDataSet = null;
    public DataSet HDSDataSet
    {
        get
        {
            if (mDataSet == null)
                mDataSet = new DataSet("HDSDataSet");
            return mDataSet;
        }
    }


   

    protected void PopulateProviderTable()
    {

       
        SqlDataAdapter sqlDA = CreateProviderAdapter();
        SqlDataAdapter sqlDAphyins = CreateProviderInsAdapter();
        SqlDataAdapter sqlDAphyspecial = CreateProviderSpecialAdapter();
        SqlDataAdapter sqlDAphyspecialproc = CreateProviderSpecialProcAdapter();
       
       
        errormessage.Text = ProviderSearch;
        errormessage2.Text = SearchProviderIns;
        errormessage3.Text = SearchProvSpecialty;

       // Create Relations - Setup Key Fields
        DataColumn doctorId = HDSDataSet.Tables["PROVIDER"].Columns["PHYSICIAN_NPI"];
        DataColumn insurId = HDSDataSet.Tables["PHYSINS"].Columns["PHYSICIAN_NPI"];
        DataColumn specialtiesId = HDSDataSet.Tables["PHYS_SPECIALTIES"].Columns["PHYSICIAN_NPI"];

        // Create Relations - Build Relation
        DataRelation specialtiesRelation = new DataRelation("specialtiesRelation", doctorId, specialtiesId);
        DataRelation insurRelation = new DataRelation("insurRelation", doctorId, insurId);
        HDSDataSet.Relations.Clear();
        HDSDataSet.Relations.Add(insurRelation);
        HDSDataSet.Relations.Add(specialtiesRelation);
 
       
    }


    protected SqlDataAdapter CreateProviderAdapter()
    {
        SqlConnection conn = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand(ProviderSearch, conn);

        if
            (txtSearchCity.Text != "")
            cmd.Parameters.Add(new SqlParameter("@SearchCity", txtSearchCity.Text));
        else
            cmd.Parameters.Add(new SqlParameter("@SearchCity", string.Empty));
               
        if
            (txtSearchState.Text != "")
            cmd.Parameters.Add(new SqlParameter("@SearchState", txtSearchState.Text));
   //     else
    //        cmd.Parameters.Add(new SqlParameter("@SearchState", string.Empty));

        if (txtSearchZip.Text != "")
            cmd.Parameters.Add(new SqlParameter("@SearchZip", txtSearchZip.Text));
   //     else
     //       cmd.Parameters.Add(new SqlParameter("@SearchZip", string.Empty));

       
        SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
       
        sqlDA.FillSchema(HDSDataSet, SchemaType.Mapped, "PROVIDER");
        sqlDA.Fill(HDSDataSet, "PROVIDER");

        return sqlDA;
    }

    private String ConnString
    {
        get
        {
            //HDS ConnectionString comes from the Web.Config
            return ConfigurationManager.ConnectionStrings["Chart RelayConnectionString"].ConnectionString;

        }
    }

    private String ProviderSearch
    {
        get
            {
                 String srchCmd = "Select PHYSICIAN_NPI, LAST_NAME, FIRST_NAME, ADDRESS_1, CITY, STATE, PHONE from Provider";

                 string whereClause = string.Empty;

                 if (txtSearchCity.Text.Length > 0)
                    whereClause = " WHERE CITY = @SearchCity";
                 if (txtSearchState.Text.Length > 0)
                      {
                         if (whereClause.Length > 0)
                              whereClause += " AND ";

                         else
                             whereClause = " WHERE ";
                             whereClause += "STATE = @SearchState";
                      }
                 if (txtSearchZip.Text != "")
                      {
                         if (whereClause.Length > 0)
                             whereClause += " AND ";

                         else
                             whereClause = " WHERE ";
                             whereClause += "ZIP = @SearchZip";
                      }
                      return srchCmd + whereClause + " ORDER BY WEIGHT";
            }
    }



    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
            cmd.Parameters.Add(new SqlParameter("@SearchInsur", string.Empty));

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

    protected SqlDataAdapter CreateProviderSpecialAdapter()
    {
        SqlConnection connPhysSpecial = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand(SearchProvSpecialty, connPhysSpecial);

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

        SqlDataAdapter sqlDAphyspecial = new SqlDataAdapter(cmd);
        sqlDAphyspecial.FillSchema(HDSDataSet, SchemaType.Mapped, "PHYS_SPECIALTIES");
        sqlDAphyspecial.Fill(HDSDataSet, "PHYS_SPECIALTIES");

        return sqlDAphyspecial;
    }

    protected SqlDataAdapter CreateProviderSpecialProcAdapter()
    {
        SqlConnection connPhysSpecialProc = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand(SearchProvSpecialtyProc, connPhysSpecialProc);

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

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


   

   

    private String SearchProviderIns
    {
        get
        {
            // String srchCmd = "Select * from PHYSINS";
            String srchCmd = "Select * from PHYSINS where INSURANCE_CARRIER_NAME = @SearchInsur AND PHYSICIAN_NPI in(SELECT PHYSICIAN_NPI FROM Provider)";
            srchCmd += ProviderSearch;

            if (txtSearchInsur.Text != "")
                srchCmd += " where INSURANCE_CARRIER_NAME = @SearchInsur";
           
            return srchCmd;

        }
    }

    private String SearchProvSpecialty
    {
        get
        {
            String srchCmd = "Select * from PHYS_SPECIALITIES";

            if(txtSearchSpecial.Text != "")
                srchCmd += " where DESCRIPTION = @SearchSpecialty";

            return srchCmd;

        }
    }

    private String SearchProvSpecialtyProc
    {
        get
        {
            String srchCmd = "Select * from SPECIAL_PROCEDURES";

            if (txtSearchSpecialProcCode.Text != "")
                srchCmd += " where CPT = @SearchSpecialtyProc";

            if (txtSearchSpecialProcDesc.Text != "")
                srchCmd += " where DESCRIPTION = @SearchSpecialtyProc";

            return srchCmd;

        }
    }
0
 

Author Closing Comment

by:kwh3856
ID: 31410236
After I reconfigured it properly I got it to work with your idea.  Thanks
0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20327964
Is it working for you now?
0
 

Author Comment

by:kwh3856
ID: 20328357
DotNetThinker,
Yes, it is working now but I have another problem with the search not returning the expected results.  Please look at my post to see if you have any ideas.

Thanks
Kenny
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 121
XML to SQL Table using c# 5 47
Data Saving 5 40
XML & .net 5 21
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

762 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

21 Experts available now in Live!

Get 1:1 Help Now