app.config中的Mysql的ConnectionStrings生成NullReferenceException

时间:2014-03-15 13:18:50

标签: c# mysql .net ado.net

NullReferenceExeception

获取 connectionString 时,

app.config正在生成

连接字符串我创建为:

<connectionStrings>
    <add name="Connect" connectionString="server=localhost;port=3306;user id=customer;password=hassaan09;database=customer" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

但是检索连接字符串异常正在生成我使用的:

public List<customer> LoadGridView()
{
    List<customer> lstCustomer = new List<customer>();

    try
    {
        string connectionString = ConfigurationManager.ConnectionStrings["Connect"].ConnectionString; //Here exception is generating
        // string connectionString = "server=localhost;port=3306;user id=customer;password=hassaan09;database=customer"; //if i directly provide connectionstring it will work fine  
        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            using (MySqlCommand cmd = new MySqlCommand("Select customerid,name,email,age from customerinfo", conn))
            {
                conn.Open();
            }
        }
    }
    catch(Exception ex)
        { MessageBox.Show(ex.ToString()); }
    return lstCustomer;
}

0 个答案:

没有答案