Solved

The variable 'sql' is assigned but its value is never used

Posted on 2009-05-13
3
629 Views
Last Modified: 2013-12-17
Good afternoon expert,

Getting error messages in VS 2005
The variable 'sql' is assigned but its value is never used
The variable 'connStringl' is assigned but its value is never used

But they are used at
dsToolkit.SelectCommand = sql;
and
dsToolkit.ConnectionString = connString;

So why does th compiler complain?

Allen



using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;
 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        //Default SQL statement which will be executed when no search conditions were entered

        String sql = "SELECT tblSecurity.Username, tblSecurity.First_Name, tblSecurity.Last_Name, tblSecurity.Company_number FROM tblSecurity";

        //Connection string - @@DBName will be replaced by the server name selected from the

        //Environment dropdown

        String connString = "DATA SOURCE = Labsdirvg600;Initial Catalog=TimeEntry;User ID=Labswirvg600;Password=Kling0n1";

        //A boolean field which is used to add a WHERE clause to the SQL statement

        //Boolean whereClauseAdded = false;
 

        //Connection string is formed here. cboEnvironment.SelectedItem.Value will return

        //the selected environment.

        //connString = connString.Replace("@@DBName", cboEnvironment.SelectedItem.Value);

    }
 

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        if (query.Checked)

            sql = "SELECT * from tblSecurity where Username Like '%sm'";

        if (query.Checked)
 

            sql = "SELECT * from tblSecurity where Username Like '%jo'";
 
 

        //Set the connection string of the SQLDataSource here

        dsToolkit.ConnectionString = connString;
 

        //Atleast one search criteria must be entered by the user if a WHERE clause needs to be

        //added
 

        //Now, the dynamically created SQL statement needs to be assigned to the SQL Data source

        dsToolkit.SelectCommand = sql;
 

        //Finally, refresh the Grid, which will cause the data source to execute the above created

        //SQL and show the results in the grid

        grdPayroll.DataBind();

    }
 

}

Open in new window

0
Comment
Question by:9apit
  • 2
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24379389
change:
 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        sql = string.empty;
        if (query.Checked)
            sql = "SELECT * from tblSecurity where Username Like '%sm'";
        if (query.Checked)
 
            sql = "SELECT * from tblSecurity where Username Like '%jo'";
 

Open in new window

0
 

Author Comment

by:9apit
ID: 24379526
Angelll,

Installed code now get
Warning      2      The variable 'sql' is assigned but its value is never used      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      16      16      C:\...\TK090514\
Warning      3      The variable 'connString' is assigned but its value is never used      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      19      16      C:\...\TK090514\
Error      4      The name 'sql' does not exist in the current context      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      31      9      C:\...\TK090514\
Error      5      'string' does not contain a definition for 'empty'      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      31      22      C:\...\TK090514\
Error      6      The name 'query' does not exist in the current context      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      32      13      C:\...\TK090514\
Error      7      The name 'sql' does not exist in the current context      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      33      13      C:\...\TK090514\
Error      8      The name 'query' does not exist in the current context      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      34      13      C:\...\TK090514\
Error      9      The name 'sql' does not exist in the current context      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      36      13      C:\...\TK090514\
Error      10      The name 'connString' does not exist in the current context      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      42      38      C:\...\TK090514\
Error      11      The name 'sql' does not exist in the current context      C:\Inetpub\wwwroot\TK090514\Default.aspx.cs      52      35      C:\...\TK090514\
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24379560
sorry, I got the error message wrong.

the problem is that you define sql in the page load, instead of the class level
using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    String sql = String.Empty; 
 

    protected void Page_Load(object sender, EventArgs e)

    {

        //Default SQL statement which will be executed when no search conditions were entered

        sql = "SELECT tblSecurity.Username, tblSecurity.First_Name, tblSecurity.Last_Name, tblSecurity.Company_number FROM tblSecurity";

        //Connection string - @@DBName will be replaced by the server name selected from the

        //Environment dropdown

        String connString = "DATA SOURCE = Labsdirvg600;Initial Catalog=TimeEntry;User ID=Labswirvg600;Password=Kling0n1";

        //A boolean field which is used to add a WHERE clause to the SQL statement

        //Boolean whereClauseAdded = false;

 

        //Connection string is formed here. cboEnvironment.SelectedItem.Value will return

        //the selected environment.

        //connString = connString.Replace("@@DBName", cboEnvironment.SelectedItem.Value);

    }

 

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        if (query.Checked)

            sql = "SELECT * from tblSecurity where Username Like '%sm'";

        if (query.Checked)

 

            sql = "SELECT * from tblSecurity where Username Like '%jo'";

 

 

        //Set the connection string of the SQLDataSource here

        dsToolkit.ConnectionString = connString;

 

        //Atleast one search criteria must be entered by the user if a WHERE clause needs to be

        //added

 

        //Now, the dynamically created SQL statement needs to be assigned to the SQL Data source

        dsToolkit.SelectCommand = sql;

 

        //Finally, refresh the Grid, which will cause the data source to execute the above created

        //SQL and show the results in the grid

        grdPayroll.DataBind();

    }

 

}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

863 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

26 Experts available now in Live!

Get 1:1 Help Now