Dear All,
I have 5 textboxs, 1 button and 1 Gridview control.
I just want that when I click the button, text of each textbox will be added in the Gridview in separate column. Each time I will click the button, a new row must be inserted in the Gridview below the last row.
Thanks in Advance!!!!
Loading
Satyapriya NayakPosted Dec 13, 2011, 6:57 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Gridview_insert._Default" %>
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 Gridview_insert
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataAdapter ad = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataTable dataTable;
SqlDataAdapter sqlda;
DataSet ds;
string str;
private void bindGrid()
{
SqlConnection conn = new SqlConnection(connStr);
dataTable = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM Vendor";
ad = new SqlDataAdapter(cmd);
ad.Fill(dataTable);
GridView1.DataSource = dataTable;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "insert into Vendor(Id,FName,LName,City,State) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
bindGrid();
Label6.Text = "Records Added";
clear();
}
void clear()
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
}
}
}
Thanks
If this post helps you mark it as answer
Dharmesh SharmaPosted Dec 13, 2011, 6:33 AM
you can do on the basis of database with update panel
create a temp table and every field store in database and load grid on button then bind
and use jquery with json if you dont want ot use database
thanks