Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query MS SQL with C#

Posted on 2009-04-08
6
Medium Priority
?
1,287 Views
Last Modified: 2013-12-17
I have added connection string to web.conf to query db with  C# to query /insert /updat to sql db. can any body help prviding me with working code that will query let's say select * from userTable. and display it on the webfile. I have set a website and havd .net framework 3.5 installed and using vs 2008.
<connectionStrings>
    <add key="ConnectionString" connectionString="Server=server; Database=db; uid=user; pwd=pass;" />
  </connectionStrings>

Open in new window

0
Comment
Question by:FD11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
ID: 24103399
From your visual studio, create a web form (let's say Test1.aspx).
Then open the code-behind file (Test1.aspx.cs), and find the Page_Load method.
Then fill it in with this code.

Btw, don't forget to include the following namespaces, if they are not yet included:
using System.Configurations;
using System.Data;
using System.Data.SqlClient;

void Page_Load(object source, EventArgs e)
{
  string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlConnection connection = new SqlConnection(connectionString);
  connection.Open();
 
  string cmdText = "select * from userTable";
  SqlCommand cmd = new SqlCommand(cmdText, connection);
  SqlDataReader reader = cmd.ExecuteReader();
 
  while (reader.Read())
  {
    Response.Write(reader["column_name"].ToString() + "<br>");
  }
 
  reader.Close();
  connection.Close();
}

Open in new window

0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24103427
something like:

 string conn_str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            var myConnection = new SqlConnection(conn_str);
            myConnection.Open();
            var mySqlDataAdapter = new SqlDataAdapter("Select Top 10 * from Transactions", myConnection);

            var myDataSet = new DataSet();
            mySqlDataAdapter.Fill(myDataSet, "Transactions");
            foreach(DataRow myDataRow in myDataSet.Tables["Transactions"].Rows)
            {
                Response.Write(myDataRow["TransactionID"].ToString());
            }
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24103443
also try to change your connectionstring to
  <add name="ConnectionString" connectionString="Server=server; Database=db; uid=user; pwd=pass;" />
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 18

Expert Comment

by:philipjonathan
ID: 24103477
As sree_ven mentioned, take note that you change 'key' to 'name' in the web.config.
<add name="ConnectionString" connectionString="Server=server; Database=db; uid=user; pwd=pass;" />
0
 

Author Comment

by:FD11
ID: 24109192
That works, thank you guys
How would I take the data from the query and display each record in my formated table such as table that I design in the .aspx file. I have done that with php, but not with .net.  links to good websites,  tutorials ..etc would be good. thanks again
0
 
LVL 9

Assisted Solution

by:Sreedhar Vengala
Sreedhar Vengala earned 500 total points
ID: 24111029
You can straight away bind dataset to GridView Control  ex: http://www.codersource.net/asp_net_grid_view_whidbey.aspx
as good way to learn is with videos (free) from : http://www.asp.net/LEARN/videos/
0

Featured Post

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 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