Inserting the values into the table without using stored procedure

Posted by Kusuma under ASP.NET on 2/15/2013 | Points: 10 | Views : 1174 | Status : [Member] | Replies : 1
Hii
I want to send the values into the database table but without using stored procedure in 3-tier architecture.
So would you please help me in the code

Thanks,
suma




Responses

Posted by: Satyapriyanayak on: 2/15/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_from_Gridview_database._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"
Text="Insert Records" />
<asp:Button ID="btn_show" runat="server" onclick="btn_show_Click"
style="height: 26px" Text="Show Records" />
</div>
</form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Insert_data_from_Gridview_database
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;

protected void btn_show_Click(object sender, EventArgs e)
{
DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("student.xml"));
GridView1.DataSource = reportData;
GridView1.DataBind();
}

protected void btn_insert_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
SqlConnection con = new SqlConnection(connStr);
com = new SqlCommand("insert into student(sid,sname,smarks,saddress) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();

}
Label1.Text = "Records inserted successfully";
}
}
}


If this post helps you mark it as answer
Thanks

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

Login to post response