-4

I've been trying to deal with this for a few hours. Here's my code.

To save some space I need to shorten my code. It seems that in my insert I cannot see the Tlat variable which is enclosed in the select statement. How can I use that variable so i can insert it.

Thanks.

SOME CODE HERE............... 
string tlocquery = "SELECT LAC,CID,LONG,LAT,STREET,MUNICIPAL FROM TRIANGULATION WHERE lat LIKE " + "'%" + tlatcut.ToString() + "%' AND long LIKE '%" + tlongcut.ToString() + "%'";
                SqlCommand commandtloc = new SqlCommand(tlocquery, cnn);
                SqlDataReader sdrtloc = commandtloc.ExecuteReader();
                while (sdrtloc.Read())
                {
                    string Tlong = sdrtloc["LONG"].ToString();
                    txtbxTlong.Text = Tlong.ToString();
                    string Tlat = sdrtloc["LAT"].ToString();
                    txtbxTlat.Text = Tlat.ToString();

                    string Lac = sdrtloc["LAC"].ToString();
                    txtbxLac.Text = Lac.ToString();
                    string Cid = sdrtloc["CID"].ToString();
                    txtbxCid.Text = Cid.ToString();

                    string Street = sdrtloc["STREET"].ToString();
                    string Municipal = sdrtloc["MUNICIPAL"].ToString();
                    txtbxTloc.Text = Street.ToString() + "," + Municipal.ToString();
                }
  SOME CODE HERE............... 
  string insertquery = "INSERT INTO TBLCURRENT (TLONG,TLAT) VALUES ('" + Tlong + "','" + Tlat + "')";
                SqlCommand insertcomm = new SqlCommand(insertquery, cnn);
                SqlDataReader insertreader = insertcomm.ExecuteReader();
CheAnne
  • 65
  • 5
  • What is `Tlat` exactly? Where it's defined? – Soner Gönül May 19 '15 at 12:58
  • 2
    You are declaring `Tlat` within a `while` loop and then trying to use it outside that loop - do you just want the _last_ `Tlat` value? – D Stanley May 19 '15 at 13:00
  • Or perhaps your insert should be within your loop? What behaviour are you actually expecting? – James Thorpe May 19 '15 at 13:01
  • @CheAnne it is not necessary to use the `ToString()` method of a `string`. I does not make it more a `string`. So for example `TLong.ToString()` is not necessary. – abto May 19 '15 at 13:09

4 Answers4

1

Each variable declared in the while loop in local to the loop. In other words, it doesn't exist anymore after the loop ends.

You'll need to declare and initialize your string outside the loop :

string Tlat = String.Empty;
while(...)
{
    //Do stuff with Tlat
} 

Then proceed with your loop to fill the variable.

Chostakovitch
  • 965
  • 1
  • 9
  • 33
1

The problem is that your Tlat variable is declared inside the while loop.

You have 2 possibilities:

  1. Restrict the usage of your Tlat variable in your loop.
  2. Put the Tlat declaration before the while loop.
Sébastien Sevrin
  • 5,267
  • 2
  • 22
  • 39
1

You are declaring the string variable Tlat in the loop. So it does not exist outside, you cannot access it after the loop here:

string insertquery = "INSERT INTO TBLCURRENT (TLONG,TLAT) VALUES ('" + Tlong + "','" + Tlat + "')";

So declare it before the loop:

string Tlat = null;
while (sdrtloc.Read())
{
    // ...
    Tlat = Tlat.ToString();
}

Now you can access it afterwards.

But if you expect only a single record, why do you use the loop at all? You could use this:

if (sdrtloc.HasRows)
{
    sdrtloc.Read();
    string Tlat = sdrtloc["LAT"].ToString();
    // ....
    string insertquery = "INSERT INTO TBLCURRENT (TLONG,TLAT) VALUES ('" + Tlong + "','" + Tlat + "')";

}

Apart from that, never use string concatenation to build your sql queries. Use sql-parameters to prevent sql-injection and other issues.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Your problem is one of scope: Because you're declaring Tlat within your while loop, it won't exist outside your while loop. So you need to declare Tlat before you enter your while loop, then modify it inside the loop, and you'll be able to access it afterwards - just take note that it'll only carry the data from your last Read operation.