Text box contents ,Grid view

Posted by Raja_89 under C# on 9/16/2013 | Points: 10 | Views : 924 | Status : [Member] | Replies : 2
Hai
Controls Used: 3 Textboxes,Grid view,Link Label
Is it possible to get the details which was entered in textbox should be displayed in Grid view in ASP.NET C#
and stored in database

Regards




Responses

Posted by: Satyapriyanayak on: 9/16/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_confirmation._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="Name" Width="60px"></asp:Label>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Address" Width="60px"></asp:Label>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="Salary" Width="60px"></asp:Label>
<asp:TextBox ID="txtsal" runat="server"></asp:TextBox><br />
</div>
<asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"
Text="Insert" OnClientClick="return confirm('Are you sure want to insert this record?');"/>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</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_confirmation
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;


protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
com = new SqlCommand("insert into employee values(@name,@address,@sal)", con);
com.Parameters.AddWithValue("@name", txtname.Text);
com.Parameters.AddWithValue("@address", txtaddress.Text);
com.Parameters.AddWithValue("@sal", txtsal.Text);
com.ExecuteNonQuery();
com.Dispose();
con.Close();
Response.Write("Records successfully inserted");
clear();
bindgrid();
}
void clear()
{
txtname.Text = "";
txtaddress.Text = "";
txtsal.Text = "";
}

protected void Page_Load(object sender, EventArgs e)
{

}
private void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
GridView1.DataMember = "employee";
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}
}


If this post helps you mark it as answer
Thanks

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

Posted by: Bandi on: 9/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
design aspx page
<body>

<form id="form1" runat="server">
<div>

<%--three labels and three textboxes--%>
<asp:Label ID="Label1" runat="server" Text="Roll" Width="60px"></asp:Label>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Name" Width="60px"></asp:Label>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="Marks" Width="60px"></asp:Label>
<asp:TextBox ID="txtsal" runat="server"></asp:TextBox><br />

<%--one link button to insert record into database--%>
<asp:LinkButton Text="Insert" ID="lnkbtn_insert" runat="server" onclick="lnkbtn_insert_Click"
OnClientClick="return confirm('Are you sure want to insert this record?');" />

<%--Display data in the grid view--%>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>

</div>
</form>
</body>


//Codebehind aspx.cs
        protected void Page_Load(object sender, EventArgs e)

{

}
string strConnString = "Data Source=XXXXX; Initial Catalog=XXXXX; User Id=testLogin; Password=xxxx@1234";
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;


protected void lnkbtn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
com = new SqlCommand("insert into Student values(@Roll,@name,@Marks)", con);
com.Parameters.AddWithValue("@Roll", txtname.Text);
com.Parameters.AddWithValue("@name", txtaddress.Text);
com.Parameters.AddWithValue("@Marks", txtsal.Text);
com.ExecuteNonQuery();
com.Dispose();
con.Close();
Response.Write("Records successfully inserted");
clear();
bindgrid();
}

void clear()
{
txtname.Text = "";
txtaddress.Text = "";
txtsal.Text = "";
}

private void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from Student";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "student");
GridView1.DataMember = "student";
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Login to post response