insert, delete and update data in asp.net [Resolved]

Posted by Lokeshbatra under Sql Server on 3/3/2012 | Points: 10 | Views : 263741 | Status : [Member] | Replies : 7
how to insert data in table under database at run time in asp.net when the aplication is shown in the browser

Lokesh Batra


Responses

Posted by: Sheonarayan on: 10/20/2014 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
To insert a record into the database table, use the code snippet mentioned in this article - http://www.dotnetfunda.com/articles/show/2708/3-tier-architecture-in-aspnet-a-complete-article.

Read the complete article however focus on 1st code snippet (DAL) that has Insert method to insert record into the database that uses stored procedure (code of this is also written in that article).

Hope this helps.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Lokeshbatra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramnandha on: 5/20/2012 [Member] Starter | Points: 25

Up
0
Down
TO INSERT RECORD IN ASP.NET USING SQL SERVER:

STRING CONSTR="CONNECTIONSTRING";

SQLCONNECTION CON=NEW SQLCONNECTION(CONSTR);
STRING SQLQUERY="INSERT INTO EMPDETAILS VALUES('" + TEXTBOX1.TEXT +"' ,'" + TEXTBOX2.TEXT + "');
CON.OPEN();
SQLCOMMAND CMD=NEW SQLCOMMAND(SQLQUERY,CON);
CMD.EXECUTENONQUERY();
CON.CLOSE();


HOPE THIS WILL HELP
REGARDS
SRIRAM.R

sriram

Lokeshbatra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramnandha on: 5/20/2012 [Member] Starter | Points: 25

Up
0
Down
TO SELECT RECORDS IN SQL SERVER WITH ASP.NET

STRING CONSTR="CONNECTIONSTRING"; 

SQLCONNECTION CON=NEW SQLCONNECTION(CONSTR);
STRING SQLQUERY="SELECT * FROM TABLENAME";
CON.OPEN();
SQLDATAADAPTER ADP=NEW SQLDATAADAPTER(SQLQUERY,CON);
DATASET DS=NEW DATASET();
ADP.FILL()
DATAGRID.DATASOURCE=DS;
DATAGRID.DATABIND();



HOPE THIS WILL HELP
REGARDS
SRIRAM.R

sriram

Lokeshbatra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ajay.Kalol on: 5/21/2012 [Member] Starter | Points: 25

Up
0
Down
Select Data from Database :

SqlConnection con = new SqlConnection("Connection String");

SqlCommand cmd = new SqlCommand("Select * from Table1", con);
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DataTable DT = new DataTable();
DA.Fill(DT);

Insert Data in Database:

SqlConnection con = new SqlConnection("Connection String");

SqlCommand cmd = new SqlCommand("INSERT INTO Table1 VALUES(1,'Ajay')", con);
con.open();
cmd.ExecuteNonQuery();
con.close();


DELETE Data from Database:

SqlConnection con = new SqlConnection("Connection String");

SqlCommand cmd = new SqlCommand("Delete from Table1 where ID = 1", con);
con.open();
cmd.ExecuteNonQuery();
con.close();


Ajay
ajaypatelfromsanthal.blogspot.in

Lokeshbatra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vicky193 on: 5/26/2013 [Member] Starter | Points: 25

Up
0
Down
You can visit following link for easy code and best way to insert data into database using asp.net c#...

http://dotnet-php.blogspot.com/2013/05/insert-data-into-database-using-aspnet-c.html

Lokeshbatra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dotnetservices on: 10/4/2016 [Member] Starter | Points: 25

Up
0
Down
Below is the code behind (webpage.aspx.cs) page where we are handling three events
(insert, update and delete) separately.
Hope you find it useful.

private void btnInsert_Click(object sender, EventArgs e)

{
string connectionString = "Data Source=SERVER;initial catalog=AdventureWorks2008R2 ; User ID=sa;Password=sqlpass;Integrated Security=SSPI;";

InsertData(connectionString,
textBox1.Text.Trim(), -- column 1
textBox2.Text.Trim() -- column 2
}

private void btnUpdate_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=SERVER;initial catalog=AdventureWorks2008R2 ; User ID=sa;Password=sqlpass;Integrated Security=SSPI;";

UpdateData(connectionString,
textBox1.Text.Trim(), -- column 1
textBox2.Text.Trim(), -- column 2
hdnID.value –- ID hidden on aspx page

}
private void btnDelete_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=SERVER;initial catalog=AdventureWorks2008R2 ; User ID=sa;Password=sqlpass;Integrated Security=SSPI;";

//Delete data on the basis on primary key (ID – hidden field on aspx page)
DeleteData (connectionString,
hdnID.value –- ID hidden on aspx page
}
private void InsertData(string connectionString, string column1, string column2)
{
// define INSERT query with parameters
string query = "INSERT INTO dbo.TABLE_NAME (Column1, Column2) " +
"VALUES (@column1, @column2) ";

// create connection and command
using(SqlConnection cn = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand(query, cn))
{
// define parameters and their values
// Change the data type as per column type
cmd.Parameters.Add("@column1", SqlDbType.ColumnType, 50).Value = column1;
cmd.Parameters.Add("@column2", SqlDbType.ColumnType, 50).Value = column2;

// open connection, execute INSERT, close connection
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}

private void UpdateData(string connectionString, string column1, string column2, int id)
{
// define INSERT query with parameters
string query = "UPDATE dbo.TABLE_NAME SET Column1 =@column1, Column2 =@column2 WHERE ID = @id ";

// create connection and command
using(SqlConnection cn = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand(query, cn))
{
// define parameters and their values
// Change the data type as per column type
cmd.Parameters.Add("@column1", SqlDbType.ColumnType).Value = column1;
cmd.Parameters.Add("@column2", SqlDbType.ColumnType).Value = column2;
cmd.Parameters.Add("@id", SqlDbType.ColumnType).Value = id;

// open connection, execute INSERT, close connection
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}

private void DeleteData(string connectionString, int id)
{
// define INSERT query with parameters
string query = "DELETE FROM dbo.TABLE_NAME WHERE ID = @id ";

// create connection and command
using(SqlConnection cn = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand(query, cn))
{
// define parameters and their values
// Change the data type as per column type
cmd.Parameters.Add("@id", SqlDbType.ColumnType).Value = id;

// open connection, execute INSERT, close connection
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}

For any other query, you can hire .net developers http://www.teaminindia.co.uk/hire-asp.net-developer.htm

Lokeshbatra, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response