hi,how to access the SQL sever database using ASP.NET.i can not access the server by using the same.some errors are showing when i try to create a database.can you help me to create a database?

Dani AI

Generated

Common causes for "cannot access SQL Server from ASP.NET" are environmental or permission issues rather than a single coding mistake. Replies here show two common approaches — a Visual Studio file-driven workflow (from ) and hand-written ADO.NET code (from ). Both are valid; the missing piece in many setups is a mismatched instance, disabled network protocol, service not running, or insufficient database permissions.

A short checklist that maps to the usual error messages:

  • Confirm the SQL Server Database Engine service and (for named instances) the SQL Server Browser are running.
  • Ensure the server accepts the protocol the app uses (TCP/IP or Named Pipes) and that the OS firewall allows SQL traffic.
  • Verify the authentication mode and that the identity used by the web app has the required database permissions.
  • Test connectivity from the machine running the app using SQL Server Management Studio or sqlcmd to isolate network/server problems from ASP.NET code.

Microsoft documentation is useful for these steps: . For development, LocalDB simplifies file-based databases and avoids many attach/permission headaches: SQL Server Express LocalDB.

Coding and security notes that address gaps in many examples: create and use a dedicated application database and account (avoid using the system master), store connection strings in configuration, use parameterized commands and proper disposal (using), and call ExecuteNonQuery for INSERT/UPDATE/DELETE, ExecuteScalar for single-value queries, and ExecuteReader for result sets. When persistent errors occur, consult the SQL Server error log and Windows Application event log — those entries usually identify whether the problem is permissions, instance discovery, or protocol/firewall related.

Recommended Answers

All 2 Replies

simple on App_Data Folder
add new item, which would be Mdf File (Database);
once added opens the server explorer.

start creating tables.

next

add page to your website..

open server explorer..

Drag the table from server explorer to your aspx page..

it will generate the gridview + SqlDataSource.

behind the scene it will add connection string to web.config which is u r requirement..

once you have connection string..

now you are ready to code..

for more visit

asp.net/learn/

aspspider.info/gridviewpanda/gridview/

hope that helps

try this code to acess data from sql server

//path of database
        string connectString = "Data Source=VOSTRO\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
        SqlConnection con = new SqlConnection(connectString);
        con.Open();
        //selecting data from database        
        string sql = "select max(e_code) from employees ";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            int val =reader.GetInt32(0);
            TextBox1.Text = Convert.ToString(Convert.ToInt32(val) + 1);
        }
        con.Close();
    }
       protected void Button1_Click(object sender, EventArgs e)
    {
        string connectString = "Data Source=VOSTRO\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
        SqlConnection connection1 = new SqlConnection(connectString);
        connection1.Open();
        
        
        DateTime d1 = DateTime.Parse(dateto.Text);
        DateTime d2 = DateTime.Parse(datefrom.Text);
        TimeSpan d = d1.Date - d2.Date;
        //int nodays = d.TotalDays;


        DateTime df= DateTime.Parse((datefrom.Text));
        int mon = df.Month;
        int year = df.Year;

        string sql = "INSERT INTO leaves_app(e_code,no_days,date_from,date_to,type,reason,sanction_tl,date_tl,sanction_dir,date_dir,accepted_by,date_acc,reliever,month,year) values (@e_code,@no_days,@date_from,@date_to,@type,@reason,@sanction_tl,@date_tl,@sanction_dir,@date_dir,@accepted_by,@date_acc,@reliever,@month,@year)";
        SqlCommand cmd = new SqlCommand(sql, connection1);
        cmd.Parameters.Add("e_code", ecode.Text);
        cmd.Parameters.Add("no_days", d.TotalDays+1);        
        cmd.Parameters.Add("date_from",DateTime.Parse(datefrom.Text));
        cmd.Parameters.Add("date_to",DateTime.Parse(dateto.Text));
        cmd.Parameters.Add("type", DropDownList1.Text);
        cmd.Parameters.Add("reason", reason.Text);
        
        cmd.Parameters.Add("sanction_tl",sanctiontl.Text);
        cmd.Parameters.Add("date_tl", DateTime.Parse(sanctiontldate.Text));
        cmd.Parameters.Add("sanction_dir",sanctiondir.Text);
        cmd.Parameters.Add("date_dir", DateTime.Parse(sanctiondirdate.Text));
        cmd.Parameters.Add("accepted_by",acceptedby.Text);
        cmd.Parameters.Add("date_acc", DateTime.Parse(dateacc.Text));
        cmd.Parameters.Add("reliever", reliever.Text);
        cmd.Parameters.Add("month", mon);
        cmd.Parameters.Add("year", year);
        cmd.ExecuteScalar();
        connection1.Close();
        Server.Transfer("leavesop.aspx");
    }


this one also acess data from sql server

protected void Button1_Click(object sender, EventArgs e)
    {
                 
        string connectString = "Data Source=VOSTRO\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
        SqlConnection connection1 = new SqlConnection(connectString);
        connection1.Open();
        string sql = "INSERT INTO employees(e_code,e_fname,e_lname,role_id,password,is_male,desig_id,branch_id,PF_appl,ESI_appl,TDS_appl,bank_accno,conf_id,basic,amt_due,amt_ded) values (@e_code,@e_fname,@e_lname,@role_id,@password,@gender,@desig_id,@branch_id,@PF_appl,@ESI_appl,@TDS_appl,@bank_accno,@conf_id,@bas,@amt_due,@amt_ded)";
        SqlCommand cmd = new SqlCommand(sql, connection1);
        cmd.Parameters.Add("e_code", TextBox1.Text);
        cmd.Parameters.Add("e_fname", first_name.Text);
        cmd.Parameters.Add("e_lname", last_name.Text);
        cmd.Parameters.Add("role_id", DropDownList1.Text);
        cmd.Parameters.Add("password",TextBox1.Text + first_name.Text);
        //extra default values
        cmd.Parameters.Add("desig_id", 1);
        cmd.Parameters.Add("branch_id", 1);
        cmd.Parameters.Add("PF_appl", false);
        cmd.Parameters.Add("ESI_appl", false);
        cmd.Parameters.Add("TDS_appl", false);
        cmd.Parameters.Add("bank_accno", -1);
        cmd.Parameters.Add("conf_id", 1);
        cmd.Parameters.Add("bas", '0');
        cmd.Parameters.Add("amt_due", '0');
        cmd.Parameters.Add("amt_ded", '0');

        if (gender1.Checked)
            cmd.Parameters.Add("gender", true);
        else
            cmd.Parameters.Add("gender", false);
        cmd.ExecuteScalar();
        connection1.Close();
        Server.Transfer("homeadmin.aspx");

    }

this code will take data from database and campare to perform operation

protected void Button1_Click(object sender, EventArgs e)
{
        //DateTime dt = DateTime.Parse(Textbox1.Text + "/01" + "/" + Textbox2.Text);
        string employeedesigid = "";
        string desgname=null;
        bool pfap = true,esiap = true;
        int bas =0;
        string connectString = "Data Source=VOSTRO\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
        SqlConnection con = new SqlConnection(connectString);      
        con.Open();
        string sql1 = "select * from employees";
        SqlCommand cmd1 = new SqlCommand(sql1, con);
        SqlDataReader reader1 = cmd1.ExecuteReader();    
        string employeename = "no employee with this id";
        
        while (reader1.Read())
        {
            if (reader1[0].ToString() == TextBox1.Text)
            {
                employeename = reader1[2].ToString() + " " + reader1[3].ToString() + " " + reader1[4].ToString();
                employeedesigid = reader1["desig_id"].ToString();
                 string ecd = reader1["e_code"].ToString();
                 pfap = bool.Parse(reader1["PF_appl"].ToString());
                 esiap = bool.Parse(reader1["ESI_appl"].ToString());
                 bas = int.Parse(reader1["basic"].ToString());
            }
        }
        con.Close();
        if (!employeename.Equals("no employee with this id"))
        {
            con.Open();
            string sql2 = "select * from designation";
            SqlCommand cmd2 = new SqlCommand(sql2, con);
            SqlDataReader reader2 = cmd2.ExecuteReader();
            while (reader2.Read())
            {
                if (reader2[0].ToString() == employeedesigid)
                {
                    desgname = reader2[1].ToString();
                }
            }

            con.Close();
            con.Open();
            int noleaves = 0;
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.