C# .net connection string

Posted on 2010-03-25
Medium Priority
Last Modified: 2012-05-09
Hi, I have created several sites in VB, but now need to create / convert a few more to C#.
I want to link a grid view to a dataset that I have, I have done it many times in vb but never in c#.
I have set up a data connection string in the web config which works as I have tested it against an SQLdatasource component embeded into the page.
I want to do this more programatically in the source so I have taken some of my VB script and placed it trough a convertor. and got out the other side the script attached. However I get an issue
"Non-invocable member 'System.Configuration.ConfigurationManager.ConnectionStrings' cannot be used like a method." when I try to bind to the gridview.
Any suggestions
protected void Gridview1_Set()
      string connStr = ConfigurationManager.ConnectionStrings("acVitaConnectionString").ConnectionString;
    SqlConnection con = new SqlConnection(connStr);
    SqlCommand SQLCmd = new SqlCommand("SPTopProducts", con);
        SQLCmd.CommandType = CommandType.StoredProcedure;

        GridView1.DataSource = SQLCmd.ExecuteReader();
    catch (Exception ex)

Open in new window

Question by:Simon Cripps
LVL 22

Accepted Solution

Om Prakash earned 2000 total points
ID: 28538782
string connStr = ConfigurationManager.ConnectionStrings("acVitaConnectionString").ConnectionString;
string connStr = ConfigurationManager.ConnectionStrings["acVitaConnectionString"].ConnectionString;

Expert Comment

ID: 28539051
you can use from System.Configuration namespace
string connStr = ConfigurationSettings.AppSettings["acVitaConnectionString"];

Author Closing Comment

by:Simon Cripps
ID: 31706977
did the job many thanks

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question