Sir I want to connect sql server database as a public in asp.net C#
i'm connected sql server in Web.Config file but i don't know how to use as a public in full project help me sir
vipin.luhach 0 Newbie Poster
shravan24 0 Newbie Poster
Create a public class which contains the connection string and new connection creation in a single method.
use the public class method in full project any where.
public class DBConnectionClass
{
public Sqlconnection GetConnection()
{
string ConnectionString=System.Configuration.ConfigurationMangare.Connectionstring["ConnectionStringNameinWeb.config"].tostring();
Sqlconnection Cn=new SqlConnection(ConnectionString);
return Cn;
}
}
It may helps you for my expectations.
Show me your code I can helps you little bit more,if your not did with the above code.
Always Ping Me,
Edited by kvprajapati because: Added [code] tags.
vipin.luhach 0 Newbie Poster
Web.Config file code :-
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="BankingConnectionString" connectionString="Data Source=LUHACH-PC;Initial Catalog=Banking;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true"/>
<authentication mode="Forms">
<forms loginUrl="LoginPage.aspx" name="login" protection="All"/>
</authentication>
</system.web>
</configuration>
DB_Connection.cs File Code :-
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class DB_Connection
{
public DB_Connection()
{
string ConnectionString = System.Configuration.ConfigurationMangare.Connectionstring["BankingConnectionString"].tostring();
Sqlconnection con = new SqlConnection(ConnectionString);
return con;
}
}
Login Page :-
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Security.Cryptography;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginButton_Click(object sender, EventArgs e)
{
try
{
//SqlConnection con = new SqlConnection("Data Source=LUHACH-PC;Initial Catalog=Banking;Integrated Security=True");
//con.Open();
SqlCommand cmd = new SqlCommand("select * from Login_Details where UserName=@Name and Password=@Password", con);
cmd.Parameters.AddWithValue("@Name", UserName_TextBox.Text);
cmd.Parameters.AddWithValue("@password", Password_TextBox.Text);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
Response.Redirect("Home_Page.aspx");
}
else
{
Messag.Text = "Enter Correct UserName & Password";
}
}
catch(SqlException ex)
{
Messag.Text = ex.Message;
}
}
}
plzzz help me and show me how to use connection use as a public...
Edited by Ezzaral because: Added code tags. Please use them to format any code that you post.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.