How to make database connectivity in asp.net [Solved/Closed]

Posts
10
Registration date
Tuesday January 27, 2009
Last seen
July 6, 2009
- - Latest reply:  ARVIND - Apr 2, 2018 at 05:23 AM
Hello,
i m looking for the database connectivity in asp.net with sql server 2005 or ms access 2007?
please anybody solve my query.
thanks in advance, looking for your response.
See more 

16 replies

Best answer
375
Thank you
hi,
please add the follwoing code in you aspx.cs page.

Step 1 : Add Namspace "using System.Data.SqlClient;"

Step 2 : Make Sql connection.

Write this code to create Sql connection.
SqlConnection con = new SqlConnection("Server=You server name or comp name;Database=Yourdatabasename;Trusted_Connectopn=True");
SqlCommand cmd = new SqlCommand("Write your sql query here eg. select * from Table name");
con.Open();
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();

i hope this should work

Say "Thank you" 375

Glad we were able to help! Love us? Write us a review! Rate CCM

CCM 2937 users have said thank you to us this month

that code must be write in page load method
this code is use full thank you
this code was very helpful
it shows error on con.open(); as sqlexception was unhandled
90
Thank you
Hi, following is code for database connectivity in asp.net using C# and and SQL Server.

While creating the database connection, consider the following things in mind.

1) Add the namespace
- System.Data
- System.Data.SqlClient.

2) Create connection object using connection parameters like server name, database name , user name and password.


3) Create sqlcommand object and pass connection object and sql query as parameters

4) Execute the command using ExecuterReader() method of sqlcommand object.
this will return resultset and get it into sqldatareader object.

5) Navigate the resultset.

6) close the connection object as well as command object

Following is the code:

Using System.Data;
Using System.Data.SqlClient;



SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader rdr ;
try
{
con = new SqlConnection("Data Source = Server Name; Initial Catalog = Database Name; User Name = user name; password = password");

con.Open();
cmd = new SqlCommand(con,"sql query");
rdr = cmd.ExecuteReader();
while(rdr.Read())
{
// Operate on fetched data
}
}
catch(Exception exp)
{
MessageBox(exp.Message);
}
finally
{
if(cmd != null)
cmd.Close();
if(con != null)
con.Close();
if(rdr != null)
rdr.Dispose();
}
I write like this .but error
con= new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=POS; Integrated Security=SSPI");
Integrated Security should be true
55
Thank you
System.Data.SqlClient;
SqlConnection conn = new SqlConnection("Data Source=servername;Initial Catalog=DatabaseName;User ID=sa;password=pwd;Integrated Security=True");



Open Connection.
User SqlDataAdapter if
52
Thank you
Im Having The Same Sort Of Problem With MySql Im Using This Code below And It Keeps Showing Errors On Line 30 If I Can Fix It It May Work For You Too Worth A Shot ?: For A P-Server Call Conquer Online If Your Wondering But I Dont Know If You Can Take Bits Out And Your For Yourself

using System;
using MySql.Data.MySqlClient;

namespace CoEmu_v2_LoginServer.Database
{
/// <summary>
/// Provides MySql resource connections, for multiple connections to a single MySql database.
/// This is due to the fact that the server is multi-threaded, so allowing a single connection would
/// likely result in errors, or general loss of performance.
/// </summary>
public static class DatabaseConnection
{
public const string USER_NAME = "localhost";
public const string PASSWORD = "1234";
public const string SERVER = "127.0.0.1";
public const string DATA_BASE = "conqueremu";
public static MySqlConnection NewConnection()
{
MySqlConnection C = null;
try{
C = new MySqlConnection("Server=" + SERVER + ";Database='" + DATA_BASE + "';Username='" + USER_NAME + "';Password='" + PASSWORD + "'");
C.Open();
}
catch(Exception.e)
{
Console.WriteLine(e.ToString());
return null;
}
return C;
}
}
}
43
Thank you
please add the following code in you aspx.cs page.

Step 1 : Add Namspace "using System.Data.SqlClient;"

Step 2 : Make Sql connection.

Write this code to create Sql connection.
SqlConnection con = new SqlConnection("Server=You server name or comp name;Database=Yourdatabasename;Trusted_Connectopn=True");
SqlCommand cmd = new SqlCommand("Write your sql query here eg. select * from Table name");
con.Open();
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();
thanks dude
really nice work thanks
tusi great ho sir
Posts
2
Registration date
Thursday May 6, 2010
Last seen
May 6, 2010
32
Thank you
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("/db/northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DataList
id="customers"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="True"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
footerstyle-font-size="9pt"
footerstyle-font-italic="True">

<HeaderTemplate>
Customers Table
</HeaderTemplate>

<ItemTemplate>
<%#Container.DataItem("companyname")%> in
<%#Container.DataItem("address")%>, <%#Container.DataItem("city")%>
</ItemTemplate>

<FooterTemplate>
Source: Northwind Database
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>
Posts
14
Registration date
Tuesday April 15, 2008
Last seen
May 29, 2009
31
Thank you
Hi there is al ink by microsoft that I think can help you much on this issue http://www.microsoft.com/sqlserver/2008/en/us/express.aspx
Thanx very much, I think I have got my task right.
27
Thank you
if you want to make connectivity with sql server on the local system.just define the connection string
sqlconnection con=new sqlconnection("server=. database="specify the datbase in your system",uid=sa;pwd=sa;");

add namespace using system.dat.sqlclient;

happy coding
Posts
40
Registration date
Tuesday March 23, 2010
Last seen
April 13, 2011
24
Thank you
Hi,
please add the following code in you aspx.cs page.

Step 1 : Add Namspace "using System.Data.SqlClient;"

Step 2 : Make Sql connection.

Write this code to create Sql connection.
SqlConnection con = new SqlConnection("Server=You server name or comp name;Database=Yourdatabasename;Trusted_Connectopn= True");
SqlCommand cmd = new SqlCommand("Write your sql query here eg. select * from Table name");
con.Open();
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();

i hope this should work
19
Thank you
I have constructed a form in aspx (form.aspx). I have constructed datafile in access. I have imported file into root of my web file. When I view form in browser and fill in fields and press 'transmit' info stays in the form and does not connect with datafile.

Can somebody please help
12
Thank you
Use a .UDL object, textual edit to see connection string
12
Thank you
go through the book available in the market the best option for this

thanks
10
Thank you
Thanku ya.
8
Thank you
Please visit the link below for details

link
Dim myConn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
kketann
Posts
1
Registration date
Tuesday April 27, 2010
Last seen
April 27, 2010
-
download our free dating software worth over $350 for free
asp.net with sql server
software is available with full source code

www.websol-dating-software.com
what should be the data source name if I dont have any sql server . I want to use built in sever of asp.net . please tell me.
Posts
1
Registration date
Wednesday April 9, 2014
Last seen
April 9, 2014
3
Thank you
try this......

SqlConnection con = new SqlConnection(strcon);
con.Open();
ds = db.get_Data("SELECT ID, user_name, password FROM login WHERE (CAST(user_name AS varbinary(50)) = CAST('" & loginusertxt.Text.ToString().Trim() & "' AS varbinary(50))) AND (CAST(password AS varbinary(50)) = CAST('" & loginpasstxt.Text.ToString().Trim() & "' AS varbinary(50))) AND Deleted = 'False'")
SqlCommand cmd = new SqlCommand("select id,u_nm,pass from login WHERE (CAST(u_nm As varbinary(50))= CAST ('" + txtunm.Text + "' As varbinary(50)) AND (CAST(pass AS varbinary(50)) = CAST('" + txtpass.Text + "' As varbinary(50)) ) " ,con);
SqlDataAdapter adpt = new SqlDataAdapter("select id,u_nm,pass from login WHERE (CAST(u_nm As varbinary(50))= CAST ('" + txtunm.Text + "' As varbinary(50)) AND (CAST(pass AS varbinary(50)) = CAST('" + txtpass.Text + "' As varbinary(50)) ) ",con);
DataSet ds = new DataSet();
adpt.fill(ds,"login");
adpt.Fill[ds, "login"];
adpt.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0].ItemArray[1].ToString().Trim() = txtunm.Text.ToString().Trim() && ds.Tables[0].Rows[0].ItemArray[2].ToString().Trim() = txtpass.Text.ToString().Trim())
{
Response.Redirect("index.aspx");
}
}
1
Thank you
You Can Make Database Connectivity Through two ways :

1- after implementing namespace using System.Data.SqlClient;
Create SsqlConnection sqlconnection cnn=new sqlconnection(" type here the connection string of the database");

and after this you can open connection and do operations you want on this connection .

2- You can make your connection is Dynamic through the following processes :

2-1 : open web.config file
2-2 : after <AppSettings/> tag , delete this tag and write new tag with the same name to show this tage <AppSettings>.....</AppSesstings>

2-3 : write inside previous tag this line

<appSettings>
<add key="connectionString" value="Data Source Of Your Database"/>
</appSettings>

2-4 : Good , now your connection it is Dynamic And More Secure .

2-5 : To Access To This connection Open The Page.cs Such As Default.cs and write :

using System.Data.SqlClient;

public SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connectionString"].ToString());

and do your operation you want without showing your database connection to secure issues .


Good Luck .

Regards : Hussein Nasser Al-Qadri