Johnny Bones
Johnny Bones

Reputation: 8404

DateTime variable goes out of scope

I've got this block of code in a code-behind:

while (DT1.Read())
{
  //Read the record into an "array", so you can find the SProc and View names
  int MyRptID = Convert.ToInt32(DT1[0]);
  string MyRptName = DT1[1].ToString();
  string MyRptSproc = DT1[2].ToString();
  string MySQLView = DT1[3].ToString();

  if (string.IsNullOrWhiteSpace(this.txtStartDate.Text))
  {
     DateTime MyStDate = Convert.ToDateTime(this.txtStartDate.Text);
  }

  if (MyStDate != null)
  {
    cmd2.Parameters.Add("@StDate", SqlDbType.Date).Value = txtStartDate.Text;
  }

MyStDate is underlined in red, and when I hover over it I get a popup that says, "The name 'MyStDate' does not exist in the current context." Can anyone tell me why this is? Does it have to do with the fact that it's outside the bracket? If so, how can I get it to work?

Upvotes: 1

Views: 237

Answers (5)

Grant Winney
Grant Winney

Reputation: 66489

You're defining DateTime MyStDate inside the if block, so it's not accessible in the next if block (it's out of scope).

You'll want to define the variable outside the first if block, so it's accessible later in the method.

DateTime myStDate;

if (String.IsNullOrWhiteSpace(this.txtStartDate.Text))
    myStDate = Convert.ToDateTime(this.txtStartDate.Text);

if (myStDate != null)
    cmd2.Parameters.Add("@StDate", SqlDbType.Date).Value = myStDate;

Actually, it looks like you have a few issues with your code.

Try this instead of what you've currently got:

if (!String.IsNullOrWhiteSpace(txtStartDate.Text))
{
    DateTime myStDate;
    if (DateTime.TryParse("txtStartDate.Text", out myStDate))
        cmd2.Parameters.Add("@StDate", SqlDbType.Date).Value = myStDate;
}

Upvotes: 1

DROP TABLE users
DROP TABLE users

Reputation: 1955

You need to declare MyStDate before the if statement. That is why it is going out of scope.

while (DT1.Read())
{
    //Read the record into an "array", so you can find the SProc and View names
    int MyRptID = Convert.ToInt32(DT1[0]);
    string MyRptName = DT1[1].ToString();
    string MyRptSproc = DT1[2].ToString();
    string MySQLView = DT1[3].ToString();
    DateTime MyStDate;
    if (string.IsNullOrWhiteSpace(this.txtStartDate.Text))
    {
        MyStDate = Convert.ToDateTime(this.txtStartDate.Text);
    }

    if (MyStDate != null)
    {
        cmd2.Parameters.Add("@StDate", SqlDbType.Date).Value = txtStartDate.Text;
    }
}

Upvotes: 2

Cameron Tinker
Cameron Tinker

Reputation: 9789

You need to create the instance of MyStDate outside the if statement. MyStDate is not visible to anything outside the scope it's declared.

DateTime MyStDate = null;

if (string.IsNullOrWhiteSpace(this.txtStartDate.Text))
    MyStDate = Convert.ToDateTime(this.txtStartDate.Text);

if(MyStDate != null)
    cmd2.Parameters.Add("@StDate", SqlDbType.Date).Value = MyStDate;

Upvotes: 0

Xavier J
Xavier J

Reputation: 4728

DO this instead:

DateTime MyStDate;
if (string.IsNullOrWhiteSpace(this.txtStartDate.Text))
{
     MyStDate = Convert.ToDateTime(this.txtStartDate.Text);
}

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68440

The scope of MyStDate is the if condition. A variable is not visible outside its scope so you need to declare MyStDate outside of the if.

Upvotes: 3

Related Questions