user3436232
user3436232

Reputation: 53

I could not connect to database

public partial class _Default : System.Web.UI.Page
       {
    SqlConnection con =    new SqlConnection("Data Source=LENOVO;Initial Catalog=dbMACARON;Integrated Security=True");    SqlCommand com = new SqlCommand("Select * from PRODUCT");


    public void Bind()
    {
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        con.Open();
        com.Connection = con;
        com.ExecuteNonQuery();
        da.Fill(ds, "PRODUCT");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }

protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
        com.Connection = con;
        SqlDataReader dr;
        dr = com.ExecuteReader();
        string id = "0";


Source Error:


Line 31:     protected void Page_Load(object sender, EventArgs e)
Line 32:     {
Line 33:         con.Open();
Line 34:         com.Connection = con;
Line 35:         SqlDataReader dr;

It said error on my con.Open could not open database, what did i go wrong? sorry im fresh so i need guidance ,

there some source error it tells me

Upvotes: 1

Views: 123

Answers (4)

user3436232
user3436232

Reputation: 53

I HAVE FIX IT MY SELF!! BUT THANKS FOR EVERYONE FOR TRYING HELPING ME OUT !! THANKS YOU!!

what i do is i put double "\" between lenovo and ebg example in sqp connection..

EXAMPLE:

public partial class SignUp : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data Source=LENOVO\EBG; Initial Catalog=dbMACARON; integrated security= True"); SqlCommand com = new SqlCommand("Select * from CUSTOMER"); public void Bind() { SqlDataAdapter da = new SqlDataAdapter(com); DataSet ds = new DataSet(); con.Open(); com.Connection = con; com.ExecuteNonQuery(); da.Fill(ds, "CUSTOMER"); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); }

weird in this web it didt show double "\" in here... but nway thanks.

Upvotes: 0

user3458545
user3458545

Reputation: 1

Try this as it is. 1. Go to Tools menu-> folder option-> view tab and unCheck the following checkbox with name that HIDE EXTENSION FOR KNWON FILE TYPES.

2.Now crate the file as sql with extension udl i.e sql.udl. It will ask you for confirmation press Yes.

  1. Right click on that file and select open with OLEDB CORE DATASERVICE. It will open the dialog box as follows. ![enter image description here][2]

  2. Go to provider tab and select Microsoft OLEDB PROVIDER FOR SQL SERVER.. AND THEN CLICK ON NEXT BUTTON.

  3. Then copy and paste your server name from SQL SERVER MANAGEMENT STUDIO in the first textbox. on second number select which mode you are using and in the end select the database which you want to use from the dropdown.
  4. Click on TEST CONNECTION button it will show you successfull connection .

finally close that window and now open that udl file with notepad. Copy the content starting from the provider name to the end and paste it into your coding page.

Thats it. It will not show you error again.

Upvotes: 0

Muhammad Omair
Muhammad Omair

Reputation: 797

if you are using sql express edition try this:

SqlConnection con = new SqlConnection("Data Source=.\SQLExpress;Initial Catalog=dbMACARON;Integrated Security=True");

Upvotes: 1

AnkitMittal
AnkitMittal

Reputation: 164

  1. Try using port number with the DB server name
  2. What is the authentication mode? Windows Authentication or User id/Password based? If it is userId/password based, then you need to provide password also.

Upvotes: 0

Related Questions