Monday, February 13, 2012

ASP.NET mini Form with SQL Server 2000

Hi, i have created a mini form to test out easy steps and my sqlconnection but something is wrong with my connection i think...
I am running windows XP Pro SP 2. i have Microsoft VisStudio .NET 2003fully installed and now im installing SQL Server 2000 DeveloprsEdition. I just cimpleted the installaion and picked some settings:
Local Computer Installation, Create new Instance since this is thefirst time i use it, Server and Client Tools, and chose a Local ratherthe Domain option aferwards. (i have used this version before but hadto format the comp so now reinstalling everything).
My problem is this, i basically started the server and it on online. iwent to the "enterprise manager" and in the "Microsoft SQL Servers-> SQWL Server Group -> (Local)(Windows NT)-> Databases, icreated a new database called tables and added a single table just tobegin testing to see if at least its functional. i was able to do thisso far.
Now i started my .NET, and wrote a tiny program to see if theconnection to the SQL database would open, but it didnt. It alwaysfailed. i am programming an ASP.NET WebApplication. I cant open theconnection to my database. i dont know why. (The tiny piece of code iwrote is at the bottom in case you know c#). There is a note alsounder the code for the ones who will read through its simlicity
any help or walk through would be appreciated. thank u...
Sam



CODE:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebTest1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
string connection = ConfigurationSettings.AppSettings["ConnectionString"];


protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label outputBox;
protected System.Web.UI.WebControls.Label Label3;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
outputBox.Text = "Button has been clicked";
SqlConnection conn = new SqlConnection(connection);

outputBox.Text += "------Creating sql Connection";

try
{
conn.Open();
outputBox.Text += "-----opening the Connection";
}
catch(Exception p)
{
outputBox.Text += "-----failed to open Connection";
Console.WriteLine(p.ToString());
}
outputBox.Text += "----Success Connection and Login to Server!";

}
}
}


NOTE: i always get this output :
Button has been clicked------Creating sqlConnection-----failed to open Connection----SuccessConnection and Login to Server!
it skips the try block's print line which means it didnt work.....

string connection =ConfigurationSettings.AppSettings["ConnectionString"]; this one iupdated in my Web.config and included application setting line thatloox like this
<add key="ConnectionString" value="Data Source=127.0.0.1; Initial Catalog=login; User Id=sa; password=pass" />

You don't say what the error message is. Probably because you are catching it and not dealling with it. In your catch block you should output the p.Message to the outputBox for debugging purposes - Writing that to the console is not going to do any good as there isn't a Console.

You said you created a database called "tables". But your connection string is attempting to open a database called "login". If you do not have a database called "login" then the connection will fail.
I've never attempted to set the datasource to "127.0.0.1" before, so I cannot say if that is likely to be a problem. If I am connecting to the SQL Server on the local machine I always use "(local)"
Does this help?

|||

swy000 wrote:


string connection = ConfigurationSettings.AppSettings["ConnectionString"]; this one i updated in my Web.config and included application setting line that loox like this
<add key="ConnectionString" value="Data Source=127.0.0.1; Initial Catalog=login; User Id=sa; password=pass" />


I am not sure why you are using the loopback IP address 127.0.0.1 in your connection string when you can just use local host. In C# check out Programming .NET by Jeff Prosise for good sample code for Asp.net and the book written by the people who write the code for this site Coding strategies of the Asp.net team by Rob Howard and mathew Gibbs, for sample code for web applications in C#. Try the link below for sample connection strings. Hope this helps.
http://www.connectionstrings.com/

No comments:

Post a Comment