Results 1 to 2 of 2

Thread: Display values from TextBoxes inside GirdView

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Answers
    15

    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....


  2. #2
    Contributing Member
    Join Date
    May 2007
    Answers
    60

    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





     

    ropDownList ID="ddlCountry" runat="server" Width="157px" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="True">
    ropDownList>

     























    Height="24px" ToolTip="Click here to Save" />







     






    .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
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact