ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 307,212 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 1,570 people online right now. Registration is fast and FREE... Join Now!




visual studio connection with sql

 

visual studio connection with sql

fido_dido_do_it

31 Jan, 2009 - 12:18 PM
Post #1

New D.I.C Head
*

Joined: 15 Dec, 2008
Posts: 10

Hey all,

I'm using Visual Studio 2008 proffessional with SQL2008 express. Using Visual C#, and Asp.net web application.
but i cannot establish the connection or to be more specific i dont know what the problem is.
i know the server is connected, tables established and everything. Here's my code, tell me what's wrong with it and how can i solve it. if there are any paths or any thing just tell me where can i get it from.
Thank you for the Help

CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection sqlConnection1 = new SqlConnection();
            SqlCommand sqlCommand1 = new SqlCommand();
            sqlConnection1.ConnectionString = "Data Source=FADY-PC\\SQLEXPRESS";
            
sqlConnection1.Open();
sqlCommand1.ExecuteNonQuery();
sqlConnection1.Close();

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String connstr = "Data Source=FADY-PC\\SQLEXPRESS";
            SqlConnection tarek = new SqlConnection(connstr);
            String query = "insert into patient(name,ssn)values('titoo',555)";
            SqlCommand co = new SqlCommand(query, tarek);
            tarek.Open();
            co.ExecuteReader();
            tarek.Close();

        }
    }
}


if any information is missing please tell me.

User is offlineProfile CardPM
+Quote Post


fido_dido_do_it

RE: Visual Studio Connection With Sql

31 Jan, 2009 - 12:26 PM
Post #2

New D.I.C Head
*

Joined: 15 Dec, 2008
Posts: 10

Hey all,

I'm using Visual Studio 2008 proffessional with SQL2008 express. Using Visual C#, and Asp.net web application.
but i cannot establish the connection or to be more specific i dont know what the problem is.
i know the server is connected, tables established and everything. Here's my code, tell me what's wrong with it and how can i solve it. if there are any paths or any thing just tell me where can i get it from.
Thank you for the Help

CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection sqlConnection1 = new SqlConnection();
            SqlCommand sqlCommand1 = new SqlCommand();
            sqlConnection1.ConnectionString = "Data Source=FADY-PC\\SQLEXPRESS";
          
sqlConnection1.Open();
sqlCommand1.ExecuteNonQuery();
sqlConnection1.Close();

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String connstr = "Data Source=FADY-PC\\SQLEXPRESS";
            SqlConnection tarek = new SqlConnection(connstr);
            String query = "insert into patient(name,ssn)values('titoo',555)";
            SqlCommand co = new SqlCommand(query, tarek);
            tarek.Open();
            co.ExecuteReader();
            tarek.Close();

        }
    }
}


if any information is missing please tell me.
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Visual Studio Connection With Sql

31 Jan, 2009 - 12:36 PM
Post #3

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 14,944



Thanked: 519 times
Dream Kudos: 11575
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
To ensure your connection string is right check out ConnectionStrings.Com, they offer connection strings for just about every type of db system. Also, are you receiving any errors? If so you're going to need to let us know the exact message. Finally, since this is an ASP.NET application I'm moving this to the ASP.NET forum smile.gif
User is offlineProfile CardPM
+Quote Post

fido_dido_do_it

RE: Visual Studio Connection With Sql

31 Jan, 2009 - 01:25 PM
Post #4

New D.I.C Head
*

Joined: 15 Dec, 2008
Posts: 10

thanks alot
there are no errors, just that the internet explorer gives me " This page cannot be displayed"
User is offlineProfile CardPM
+Quote Post

AshishKumar_Gupta

RE: Visual Studio Connection With Sql

1 Feb, 2009 - 10:43 PM
Post #5

New D.I.C Head
*

Joined: 19 Jan, 2009
Posts: 27

just write connection string like
Data Source=FADY-PC\\SQLEXPRESS;Initial catalog=yourdatabasename;uid=ifany;pwd=ifany

Thanks and enjoy
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: Visual Studio Connection With Sql

5 Feb, 2009 - 08:58 AM
Post #6

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,846



Thanked: 205 times
Dream Kudos: 500
Expert In: .NET

My Contributions
you need to tell the SqlCommand object what it needs to do. You do not supply the query nor the CommandType.

How do you expect it to know what to do? How do you expect the object to know what query to run?

In the page load, you don't give it a query.

Also, as AshishKumar_Gupta stated, your connection string is incorrect.

This post has been edited by eclipsed4utoo: 5 Feb, 2009 - 09:08 AM
User is offlineProfile CardPM
+Quote Post

senol01

RE: Visual Studio Connection With Sql

5 Feb, 2009 - 11:26 PM
Post #7

New D.I.C Head
*

Joined: 5 Feb, 2009
Posts: 7


My Contributions
http://www.dreamincode.net/forums/showtopic83638.htm
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 09:47PM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month