Display values from TextBoxes inside GirdView
Display values from TextBoxes inside GirdView into Database
Hi,
Could anyone help me ...
I have a GridView and it has several textboxes in it.
I want to enter some values in those textboxes...and once I enter....all these values should be dumped into a database table.....
Is there a way.....if so pleaseeee let me have the complete code....
Thanx a lot in advance....
Re: Display values from TextBoxes inside GirdView
I used Ajax for Binding the Data to the dropdown as well as Inserting the data in to the database from gridview.
Here the code for both problems in sible application......
.aspx
------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" Width="157px" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
<asp:TextBox ID="txtCapital" runat="server" Style="z-index: 100; left: 13px; position: absolute;
top: 126px"></asp:TextBox>
<asp:GridView ID="gvCountry" runat="server" AutoGenerateColumns="False" Style="z-index: 100;
left: 14px; position: absolute; top: 202px" Width="418px" OnRowEditing="gvCountry_RowEditing">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label Text="Country" ID="lblCountry" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox runat="server" ID="txtCountry"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label Text="Capital" ID="lblCountry" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox runat="server" ID="txtCapital"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSave" runat="server" CommandName="Edit" Text="Save" Width="58px"
Height="24px" ToolTip="Click here to Save" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
.aspx.cs
---------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlCon;
SqlDataAdapter sqlAdap;
SqlCommand sqlCmd;
DataSet sqlDs;
string commandText="";
string conString = "Trusted_Connection=True;server=localhost;database=master";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
protected DataSet GetData(string sqlCommand)
{
sqlCon = new SqlConnection(conString);
sqlAdap = new SqlDataAdapter(sqlCommand, conString);
sqlDs = new DataSet();
sqlAdap.Fill(sqlDs);
return sqlDs;
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
commandText = "select countryCapital from country where countryName='"+ ddlCountry.SelectedValue+"'";
sqlDs = new DataSet();
sqlDs = GetData(commandText);
txtCapital.Text = sqlDs.Tables[0].Rows[0][0].ToString();
}
protected void gvCountry_RowEditing(object sender, GridViewEditEventArgs e)
{
TextBox txtCountry = (TextBox)(((GridViewRow)gvCountry.Rows[e.NewEditIndex])).FindControl("txtCountry");
TextBox txtCapital = (TextBox)(((GridViewRow)gvCountry.Rows[e.NewEditIndex])).FindControl("txtCapital");
commandText= "insert into country values('"+ txtCountry.Text+"','"+ txtCapital.Text +"')";
sqlCon = new SqlConnection(conString);
sqlCmd = new SqlCommand(commandText, sqlCon);
sqlCon.Open();
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
txtCountry.Text = "";
txtCapital.Text = "";
BindData();
}
protected void BindData()
{
commandText = "select countryName from country";
sqlDs = new DataSet();
sqlDs = GetData(commandText);
ddlCountry.DataValueField = "countryName";
ddlCountry.DataSource = sqlDs.Tables[0];
ddlCountry.DataBind();
ddlCountry.Focus();
gvCountry.DataSource = " ";
gvCountry.DataBind();
}
}
If u need any other information please let me know ASAP.