Text box contents ,Grid view

Posted by Raja_89 under C# on 9/16/2013 | Points: 10 | Views : 1571 | Status : [Member] | Replies : 1
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: 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