变量字符串变为空

时间:2014-12-11 16:45:23

标签: c# asp.net

我不明白为什么我的变量' ver'声明为字符串变为" null"在我的sql请求中,当我使用我的函数' Button1_Click',而在我的函数' Menu1_MenuItemClick'这个变量' ver'不是" null" ...

public partial class Page_Admin : System.Web.UI.Page
{
    public string ver;

    protected void Page_Load(object sender, EventArgs e)
    {    
    }

public void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{     
    if (e.Item.Parent != null)
    {
        switch (e.Item.Parent.Value)
        {
            case "BP":
            try
            {
                string url1 = "my_url";
                HtmlWeb web1 = new HtmlWeb();
                HtmlDocument doc1 = web1.Load(url1);
                var version1 = doc1.GetElementbyId("title");
                TextBox2.Text = version1.InnerHtml;
                ver = version1.InnerHtml;          
            }
            catch (Exception ex)
            {
                Label3.Text = "Error:" + ex;
            }
            break;

            case "OL":
            ..........
         }
  .............
     }
  .............
 }

public void Button1_Click(object sender, EventArgs e)
{
    string connectionString = null;
    SqlConnection connection;
    SqlCommand command;
    string sql = null;

    connectionString = @"...";

    sql = "INSERT INTO dbo.BP (appli, version, date, mode, variante) VALUES ('test','" + ver +"','11/12/2014','connecté','N+1');";

    connection = new SqlConnection(connectionString); 
    connection.Open();
    command = new SqlCommand(sql, connection);
    SqlDataReader reader = command.ExecuteReader();
    command.Dispose();
    connection.Close();
}

0 个答案:

没有答案