locked
WCF Data Binding using Ado.net RRS feed

  • Question

  • User633205417 posted

    I am using wcf to develope a web service.

    In this, I need to fetch few records from database using Ado.net.

    I have used Wcf Service Library to create a web service.

    The problem here I am getting is when I am trying to communicate with the Database it is giving me some weired errors.

    For ex:-

    When I am trying to use Connection String,

    string ConString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(ConString); // it give me error here in ConString

    Error:-  A field initializer cannot refrence the non-static field, method or property "ProjectName.ClassName.ConString"

    But this works fine in normal Asp.net Web Application.

    or when I am using like this,

    string ConString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter ada;
    DataSet ds;

    using(con = new SqlConnection(ConString))

    After using nothing comes in Intellisense.

    Please help me, am i getting wrong some where.

    Thanks in advance.

    Thursday, October 11, 2012 6:01 AM

Answers

  • User1999579388 posted

    string ConString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(ConString); // it give me error here in ConString

    Hi,

    Where do you use above code in your project? It seems that it requires the things to the right of '=' be static variables. 

    You could follow below thread to modify your code if you use them in public partial class : System.Web.UI.Page.

    Please see: http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/feece9b4-326b-4dff-a97c-43570c348593/ 

    If not, please post that part of code.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 18, 2012 3:33 AM

All replies