Database Connectionstring connect from class library file
if we need Database connectionstring get directly from webform cs file but this concept call from Class.cs file for hiding connection information using Asp.Net C#.
DEMO
Class.cs File
Next - Add namespace and call connection string to sqlconnection
Next - Call the class.cs library file to Webform Codebehind file below like this
if we need Database connectionstring get directly from webform cs file but this concept call from Class.cs file for hiding connection information using Asp.Net C#.
DEMO
HTML CODING
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form runat="server">
<div align="center">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="View" />
<br />
<asp:GridView ID="GridView1" runat="server"></asp:GridView></div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Class1
/// </summary>
///
public class dbconnection
{
public SqlConnection connection()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
con.Open();
return con;
}
}
C# Coding
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;
using System.Data;
public partial class AngularJS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
dbconnection dbc = new dbconnection();
SqlConnection con = dbc.connection();
SqlCommand cmd = new SqlCommand("select * from
register", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
con.Close();
}
}
First add Class file - Right click - Add - Add New Item - add -Class.cs file
add -Class.cs file
Next - Add namespace and call connection string to sqlconnection
Next - Call the class.cs library file to Webform Codebehind file below like this
0 comments:
Post a Comment