Database Connectivity Using Sql Server Database

Database Connectivity  Using Sql Server Database(ADO.NET)




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ConnectivityProgramSimpleMethod: System.Web.UI.Page
{
    protected voidbtnUpload_Click(object sender, EventArgs e)
    {
        TextBox3.Text = "~/Images/"+ FileUpload1.FileName;
        FileUpload1.SaveAs(Server.MapPath(TextBox3.Text));
        Image1.ImageUrl = TextBox3.Text;
    }
    protected voidbtnInsert_Click(object sender, EventArgs e)
    {
        SqlConnection conn=newSqlConnection(@"Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True");
        conn.Open();
        String InsertCmd = "Insert Into ProductTable (ProductName,ProductImage,Remarks) Values('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')";
        Response.Write(InsertCmd);
        SqlCommand cmd = newSqlCommand(InsertCmd, conn);
        int x = cmd.ExecuteNonQuery();
        Response.Write("insert Record"+ x);
        conn.Close();
    }
    protected voidbtnDelete_Click(object sender, EventArgs e)
    {
        SqlConnection conn = newSqlConnection(@"Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True");
        conn.Open();
        String deleteCmd = "delete from ProductTable  where ProductId="+ TextBox1.Text;
        Response.Write(deleteCmd);
        SqlCommand cmd = newSqlCommand(deleteCmd, conn);
        int x = cmd.ExecuteNonQuery();
        Response.Write("Delete Record"+ x);
        conn.Close();
    }
    protected voidbtnUpdate_Click(object sender, EventArgs e)
    {
        SqlConnection conn = newSqlConnection(@"Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True");
        conn.Open();
        String UpdateCmd = "update ProductTable set ProductName='" + TextBox2.Text + "',   ProductImage='" + TextBox3.Text + "', Remarks='" + TextBox4.Text + "' where ProductId="+TextBox1.Text;
        Response.Write(UpdateCmd);
        SqlCommand cmd = newSqlCommand(UpdateCmd, conn);
        int x = cmd.ExecuteNonQuery();
        Response.Write("Update Record"+ x);
        conn.Close();
    }
    protected voidbtnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection conn = newSqlConnection(@"Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True");
        conn.Open();
        Stringsearchcmd = "select *from ProductTable where ProductId=" + TextBox1.Text;
        SqlDataAdapter da = newSqlDataAdapter(searchcmd, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        TextBox2.Text = ds.Tables[0].Rows[0]["ProductName"].ToString();
        TextBox3.Text = ds.Tables[0].Rows[0]["ProductImage"].ToString();
        TextBox4.Text = ds.Tables[0].Rows[0]["Remarks"].ToString();
        Image1.ImageUrl = TextBox3.Text;
 }
protected void btnSearchGridview_Click(object sender, EventArgse)
{
  SqlConnection conn = newSqlConnection(@"Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True");
  conn.Open();
  String searchcmd = "select *from ProductTable where ProductId=" + TextBox1.Text;
        SqlDataAdapter da = newSqlDataAdapter(searchcmd, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        conn.Close();
     
    }
}



Comments

Popular posts from this blog

Creating a DDL File & link ddl file in C#

SQL Command in asp.net