following program compiles Successfully, but exe gives above error, kindly can someone tell what is wrong

using System;
//using System.Data;
//using System.Data.SqlClient;
public class zzz
{
    public static void Main()
    {
        try
        {
            string t;
            t = System.Environment.GetEnvironmentVariable("QUERY_STRING");
            char[] v;
            v = new char[1];
            v[0] = '=';
            string[] u;
            u = t.Split(v);
        }
        catch (System.Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        //string sqlsttm = "select * from " + u[1];
        //SqlConnection con = new SqlConnection("Server=Kings\\SqlExpress;Data=Agrasen;Trusted_Connection=true;");
        //con.Open();
        //SqlCommand com = new SqlCommand(sqlsttm, con);
        //SqlDataReader rdr = com.ExecuteReader();
        //Console.WriteLine("Content-Type:text/html\n");
        //while (rdr.Read())
        //{
        //    for (int i = 0; i < rdr.FieldCount; i++)
        //        Console.Write(rdr.GetValue(i) + " ");
        //    Console.WriteLine("<br>");
        //}

    }
}

Recommended Answers

All 6 Replies

string t;
t = System.Environment.GetEnvironmentVariable("QUERY_STRING");
}

Are you really sure that variable "t" will be initialized after calling GetEnvironmentVariable(string)? I think you shall check "t" for null and assign a default value to it before calling Split on it.
What is wrong: "t" is probably null.

Martin.

Are you really sure that variable "t" will be initialized after calling GetEnvironmentVariable(string)? I think you shall check "t" for null and assign a default value to it before calling Split on it.
What is wrong: "t" is probably null.

Martin.

Thanks Martin,

Yes it is true that in environment the Query_String not created by Apache Server, hence this exception was raised, however can u guide as
1 ? how to check for fucntions returning null values and avoid such exceptions
2 ? why Apache server does not generating Query_String environemnt variable when the html page is submited

Hi
I dont know why apache server generate Query_String. but u can avoid null like this

string t = null;
t = System.Environment.GetEnvironmentVariable("QUERY_STRING");
if ( t != null ){
   // Proceed with t, Otherwise it is null
}

Thanks Again Martin;

string tbl2rd = null;
tbl2rd = System.Environment.GetEnvironmentVariable("QUERY_STRING") ;
if (tbl2rd = null )

it is Generating compile time error
error CS0029: Cannot implicitly convert type 'string' to 'bool'
is not working.

rather the question is
? if any function returns unexpected type values as in above case a sting variable is getting null values causing System.NullReferenceException Eception to be raised how to arrest such situations ?

thanks

Hi,
U used = instead of !=. Carefully watch the code

string tbl2rd = null;
tbl2rd = System.Environment.GetEnvironmentVariable("QUERY_STRING") ;
[B]if (tbl2rd != null ) [/B]

if (tbl2rd = null )
thanks

You are assigning null to tbl2rd and then comparing tbl2rd against true or false; this won't work. You have to use == operator for comparing values or System.String.Compare(String) function.

Back to your question: It is generally enough if you check if the string is null (eg. if ((myValue = System.Environment.GetEnvironment(String)) == null) { your code here }

By the way, System.Environment.GetEnvironmentVariable() gets the variable from the current process or the user/local machine variables (ie. registry keys). Did you mean the server variables of Apache you can obtain from HttpWebRequest class (HttpWebRequest.ServerVariables)?

Take a look here: http://www.zytrax.com/tech/web/env_var.htm

Martin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.