0

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?

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ashishkj1989
0

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

0

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;

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.