using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace preApp
{
public class Register1
{
public int id { get; set; }
public string name { get; set; }
public string adress { get; set; }
public decimal phon { get; set; }
public string email { get; set; }
public decimal pin { get; set; }
public string password { set; get; }
public string rol { set; get; }
}
public class DataAccessLayer
{
public static List getData()
{
List register = new List();
string connstr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connstr))
{
SqlCommand cmd = new SqlCommand("select * from dbo.Register", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.HasRows)
{
Register1 reg = new Register1();
reg.id = Convert.ToInt16(dr["id"]);
reg.name = dr["name"].ToString();
reg.adress = dr["adress"].ToString();
reg.phon = Convert.ToDecimal(dr["phon"]);
reg.email = dr["email"].ToString();
reg.pin = Convert.ToDecimal(dr["pin"]);
reg.password = dr["password"].ToString();
reg.rol = dr["rol"].ToString();
register.Add(reg);
}
}
return register;
}
public static void delData(int id)
{
string cs = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("delete from Register where id= @id", con);
SqlParameter parm = new SqlParameter("@id", id);
cmd.Parameters.Add(parm);
con.Open();
cmd.ExecuteNonQuery();
}
}
public static int updateData(int id, string name, string adress, decimal phon, string email, decimal pin, string password, string rol)
{
string cs = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
string updateQuery = "update Register set name=@name, adress=@adress, phon=@phon, email=@email, pin=@pin, password=@password, rol=@rol where id= @id";
SqlCommand cmd = new SqlCommand(updateQuery, con);
SqlParameter parmId = new SqlParameter("@id", id);
cmd.Parameters.Add(parmId);
SqlParameter parmName = new SqlParameter("@name", name);
cmd.Parameters.Add(parmName);
SqlParameter parmAdress = new SqlParameter("@adress", adress);
cmd.Parameters.Add(parmAdress);
SqlParameter parmPhon = new SqlParameter("@phon", phon);
cmd.Parameters.Add(parmPhon);
SqlParameter parmEmail = new SqlParameter("@email", email);
cmd.Parameters.Add(parmEmail);
SqlParameter parmPin = new SqlParameter("@pin", pin);
cmd.Parameters.Add(parmPin);
SqlParameter parmPassword = new SqlParameter("@password", password);
cmd.Parameters.Add(parmPassword);
SqlParameter parmRol = new SqlParameter("@rol", rol);
cmd.Parameters.Add(parmRol);
con.Open();
return cmd.ExecuteNonQuery();
}
}
public static int insertData(string name, string adress, decimal phon, string email, decimal pin, string password, string rol)
{
string cs = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
string insertQuery = "insert into Register(name, adress, phon, email, pin, password, role) values(@name, @adress, @phon, @email, @pin, @password, @rol)";
SqlCommand cmd = new SqlCommand(insertQuery, con);
SqlParameter parmName = new SqlParameter("@name", name);
cmd.Parameters.Add(parmName);
SqlParameter parmAdress = new SqlParameter("@adress", adress);
cmd.Parameters.Add(parmAdress);
SqlParameter parmPhon = new SqlParameter("@phon", phon);
cmd.Parameters.Add(parmPhon);
SqlParameter parmEmail = new SqlParameter("@email", email);
cmd.Parameters.Add(parmEmail);
SqlParameter parmPin = new SqlParameter("@pin", pin);
cmd.Parameters.Add(parmPin);
SqlParameter parmPassword = new SqlParameter("@password", password);
cmd.Parameters.Add(parmPassword);
SqlParameter parmRol = new SqlParameter("@rol", rol);
cmd.Parameters.Add(parmRol);
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
}
Manikandan MurugesanPosted Jul 1, 2017, 10:01 AM
Sql Server fire this error when your application don't have enough rights to access the database. there are several reason about this error . To fix this error you should follow the following instruction.
Try to connect sql server from your server using management studio . if you use windows authentication to connect sql server then set your application pool identity to server administrator .
if you use sql server authentication then check you connection string in web.config of your web application and set user id and password of sql server which allows you to log in .
if your database in other server(access remote database) then first of enable remote access of sql server form sql server property from sql server management studio and enable TCP/IP form sql server configuration manager .
after doing all these stuff and you still can't access the database then check firewall of server form where you are trying to access the database and add one rule in firewall to enable port of sql server(by default sql server use 1433 , to check port of sql server you need to check sql server configuration manager network protocol TCP/IP port).
if your sql server is running on named instance then you need to write port number with sql serer name for example 117.312.21.21/nameofsqlserver,1433.
If you are using cloud hosting like amazon aws or microsoft azure then server or instance will running behind cloud firewall so you need to enable 1433 port in cloud firewall if you have default instance or specific port for sql server for named instance.
If you are using amazon RDS or SQL azure then you need to enable port from security group of that instance.
If you are accessing sql server through sql server authentication mode them make sure you enabled "SQL Server and Windows Authentication Mode" sql server instance property.