Basic Queries in Asp.net (select, Insert, Update, Delete)

In This Chapter you Will Learn How to select, insert, update and delete data form database using ASP.Net

Select Query in Asp.net

First We Have To Create table in database. Tabel with a name of demotb

now we have to Select TutorialID, TutorialName from demotb

namespace DemoApplication
{  
	public partial class Demo System.Web.UI.Page  
    {  
	  protected void Page_Load(object sender, EventArgs e)  
	  {  
		SqlCommand command;
		SqlDataReader dataReader;
		String sql, Output =" ";
		sql = "Select TutorialID,TutorialName from demotb";
		
		command = new SqlCommand(sql, cnn);
		
		dataReader = sqlquery.ExecuteReader();
		while (dataReader.Read())
		{
		  Output = Output + dataReader.GetValue(0) + "-" + dataReader.GetValue(1) + "
"; } Response.Write(Output); dataReader.Close(); command.dispose(); conn.Close(); } } }

copy the above code to select data from database

Insert data in database using insert command

follow the below code to insert data to database

namespace DemoApplication
{  
	public partial class Demo System.Web.UI.Page  
    {  
	  protected void Page_Load(object sender, EventArgs e)  
	  {  
		SqlCommand command;
		SqlDataAdapter adapter = new SqlDataAdapter(); 
		String sql="";
		
		sql = "Insert into demotb(TutorialID,TutorialName) value(3, '" + "VB.Net +"')";
		
		command = new SqlCommand(sql,cnn);
		adapter.InsertCommand = new SqlCommand(sql,cnn); 
		adapter.InsertCommand.ExecuteNonQuery();

		command.Dispose(): 
		cnn.Close();
				
	  }
	}
}

copy the above code to insert data to database

Update data in database using update command

First We Having Some Data in Database to update Records

Follow the Below Code To update data in database

Easy Way to Delete Data From Database using Delete Command

namespace DemoApplication
{  
	public partial class Demo System.Web.UI.Page  
    {  
	  protected void Page_Load(object sender, EventArgs e)  
	  {  
		SqlCommand command;
		SqlDataAdapter adapter = new SqlDataAdapter(); 
		String sql="";
		
		sql = "Delete demotb where TutorialID=3";
		
		command = new SqlCommand(sql,cnn);
		
		adapter.DeleteCommand = new SqlCommand(sql,cnn); 
		adapter.DeleteCommand.ExecuteNonQuery;
		
		command.Dispose(): 
		cnn.Close();
				
	  }
	}
}
namespace DemoApplication
{  
	public partial class Demo System.Web.UI.Page  
    {  
	  protected void Page_Load(object sender, EventArgs e)  
	  {  
		SqlCommand command;
		SqlDataAdapter adapter = new SqlDataAdapter(); 
		String sql="";
		
		sql = "Delete demotb where TutorialID=3";
		
		command = new SqlCommand(sql,cnn);
		
		adapter.DeleteCommand = new SqlCommand(sql,cnn); 
		adapter.DeleteCommand.ExecuteNonQuery;
		
		command.Dispose(): 
		cnn.Close();
				
	  }
	}
}

If this command Lines are not Working then please comment on below. then We Will Find Alternate Solutions