Solved

Must declare the scalar variable "@SearchCity"

Posted on 2007-11-20
10
620 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
[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
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 143

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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