Geeks Talk

Prepare for your Next Interview




Display values from TextBoxes inside GirdView

This is a discussion on Display values from TextBoxes inside GirdView within the ASP.NET forums, part of the Web Development category; 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 ...


Go Back   Geeks Talk > Web Development > ASP.NET

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-01-2008
Junior Member
 
Join Date: Apr 2008
Location: visakhaptnam
Posts: 18
Thanks: 2
Thanked 2 Times in 1 Post
ushalakshmi is on a distinguished road
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....
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-16-2008
Contributing Member
 
Join Date: May 2007
Location: bangalore
Posts: 65
Thanks: 3
Thanked 8 Times in 6 Posts
hari.nattuva is on a distinguished road
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>
&nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<aspropDownList ID="ddlCountry" runat="server" Width="157px" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="True">
</aspropDownList>
<asp:TextBox ID="txtCapital" runat="server" Style="z-index: 100; left: 13px; position: absolute;
top: 126px"></asp:TextBox>
&nbsp;
<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>
&nbsp;

</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.
Reply With Quote
Reply

  Geeks Talk > Web Development > ASP.NET


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
How to do populate values in Quality Center from values in Excel? snabors Quality Center 1 03-08-2008 03:14 AM
display the null values using prompt Muthu3180 Data Warehousing 1 02-21-2008 02:04 AM
Identify object inside application rcsdag QTP 3 12-18-2007 03:59 AM
Functions inside triggers rajakumar_na Oracle 1 11-16-2007 02:39 AM
assigning a value inside interface.... psuresh1982 Java 4 03-01-2007 01:03 AM


All times are GMT -4. The time now is 01:54 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved