-
Junior Member
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....
-
Contributing Member
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" %>
Untitled Page
.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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules