Monday, 30 November 2020

CRUD operation using ADO .NET ,WEB FROM

In design Page

<tr>

<td style="width: 30%; text-align: left">Name</td>

<td style="width: 70%; text-align: left">

<asp:TextBox ID="txtName" runat="server" Width="80%"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 30%; text-align: left">Roll</td>

<td style="width: 70%; text-align: left">

<asp:TextBox ID="txtRoll" runat="server" Width="80%"></asp:TextBox> </td>

</tr>

<tr>

<td >

<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />

</td>

</tr>

</tr>

<tr>

<td >

<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />

</td>

</tr>


Add connection string


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStudent_db"].ConnectionString);

<configuration>

<connectionStrings>

<add name="ConnStudent_db" connectionString="Data Source=.; Initial catalog=Student_db; Uid=sa; Pwd=anita" providerName="System.Data.SqlClient"/>

</connectionStrings>

Or

SqlConnection con = new SqlConnection("Data Source=.; Initial catalog=Student_db; Uid=sa; Pwd=anita");


To insert data


protected void btnInsert_Click(object sender, EventArgs e)

{

int roll = Convert.ToInt32(txtRoll.Text);

string name = txtName.Text;

SqlDataAdapter adp = new SqlDataAdapter("INSERT INTO STUD(ROLL,NAME) VALUES("+roll+",'"+name+"')",con);

DataTable dt = new DataTable();

adp.Fill(dt);

Clear();

}

void Clear()

{

txtRoll.Text = "";

txtName.Text = "";           

txtRoll.Focus();

}


To update data

protected void btnEdit_Click(object sender, EventArgs e)

{

GridViewRow row = (sender as Button).Parent.Parent as GridViewRow;

HiddenField hdf = (HiddenField)row.FindControl("hdnStdid");

hdnStdid1.Value = hdf.Value;

txtRoll.Text = row.Cells[0].Text;

txtName.Text = row.Cells[1].Text;

}

protected void btnUpdate_Click(object sender, EventArgs e)

{

int roll = Convert.ToInt32(txtRoll.Text);

string name = txtName.Text;

SqlDataAdapter adp = new SqlDataAdapter("UPDATE STUD SET ROLL=" + roll + ",NAME='" + name + "' WHERE STDID=" + hdnStdid1.Value + "", con);

DataTable dt = new DataTable();

adp.Fill(dt);

Clear();

ShowGrid();

}


To Delete Data

SqlDataAdapter adp = new SqlDataAdapter("DELETE FROM STUD WHERE STDID=" + hdnStdid1.Value + "", con);

DataTable dt = new DataTable();

adp.Fill(dt);

Clear();

ShowGrid();


To Show data on grid 

         

void ShowGrid()

{

SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM STUD", con);

DataTable dt = new DataTable();

adp.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

}













No comments:

Post a Comment