I'm a newbie in using C#.net language and I'm having problem with populating textboxes from a gridview row selection. In my gridview, I only have 9 columns but what I want is to fill in the textboxes with the details coming from the database based on what I selected in the gridview.
Below is my code,
string rowID = grdvwSearch.SelectedValue.ToString();
Response.Write(grdvwSearch.SelectedValue.ToString());
SqlConnection con = new SqlConnection(GetConnectionString());
SqlCommand cmd = new SqlCommand("Select * from vehicles inner join syspara3 on vehicles.model_code=syspara3.model_code " +
"inner join customer on vehicles.cust_code=customer.cust_code inner join insurance on vehicles.inscode=insurance.inscode " +
"where vinno='" + rowID + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//Response.Write("asdad");
if (ds.Tables[0].Rows.Count >= 0)
{
txtChassiNo.Text = ds.Tables[0].Rows[0]["vinno"].ToString();
txtRegNo.Text = ds.Tables[0].Rows[0]["plate_no"].ToString();
txtDateOfSale.Text = ds.Tables[0].Rows[0]["reg_date"].ToString();
txtManufacturer.Text = ds.Tables[0].Rows[0]["vehbrand"].ToString();
ddlModelCode.SelectedItem.Text = ds.Tables[0].Rows[0]["model_code"].ToString();
lblModelDesc.Text = ds.Tables[0].Rows[0]["frn_sname"].ToString();
txtModelYear.Text = ds.Tables[0].Rows[0]["model_year"].ToString();
txtEngNo.Text = ds.Tables[0].Rows[0]["engine_no"].ToString();
txtNumCylinder.Text = ds.Tables[0].Rows[0]["engpower"].ToString();
ddlTransmission.SelectedItem.Text = ds.Tables[0].Rows[0]["trantyp"].ToString();
txtExtColor.Text = ds.Tables[0].Rows[0]["ext_colour"].ToString();
txtIntColor.Text = ds.Tables[0].Rows[0]["int_colour"].ToString();
txtCustCode.Text = ds.Tables[0].Rows[0]["cust_code"].ToString();
txtCustName.Text = ds.Tables[0].Rows[0]["cust_name"].ToString();
txtBpiNo.Text = ds.Tables[0].Rows[0]["bp_idno"].ToString();
txtFileNo.Text = ds.Tables[0].Rows[0]["file_no"].ToString();
txtRemarks.Text = ds.Tables[0].Rows[0]["remarks"].ToString();
ddlTyreBrand.SelectedItem.Text = ds.Tables[0].Rows[0]["tbrand"].ToString();
ddlTyreSize.SelectedItem.Text = ds.Tables[0].Rows[0]["tyrsize"].ToString();
txtImmobCode.Text = ds.Tables[0].Rows[0]["imbcode"].ToString();
txtImmobNo.Text = ds.Tables[0].Rows[0]["imbno"].ToString();
txtRadioCode.Text = ds.Tables[0].Rows[0]["radiocode"].ToString();
txtRadioNo.Text = ds.Tables[0].Rows[0]["radiono"].ToString();
txtWheelNo.Text = ds.Tables[0].Rows[0]["wheelkeyno"].ToString();
ddlCarStatus.SelectedItem.Text = ds.Tables[0].Rows[0]["status_ds"].ToString();
txtVehclDesc.Text = ds.Tables[0].Rows[0]["car_desc"].ToString();
txtLoyaltyDateFrom.Text = ds.Tables[0].Rows[0]["loyalty1"].ToString();
txtLoyaltyDateTo.Text = ds.Tables[0].Rows[0]["loyalty2"].ToString();
txtAddlRemarks.Text = ds.Tables[0].Rows[0]["addremarks"].ToString();
txtInsuranceCode.Text = ds.Tables[0].Rows[0]["inscode"].ToString();
ddlInsuranceDesc.SelectedItem.Text = ds.Tables[0].Rows[0]["insdesc"].ToString();
txtDateCoveredFrom.Text = ds.Tables[0].Rows[0]["ins_dt"].ToString();
txtDateCoveredTo.Text = ds.Tables[0].Rows[0]["ins_dt2"].ToString();
txtNewCarBeg.Text = ds.Tables[0].Rows[0]["pw_date"].ToString();
txtNewCarEnd.Text = ds.Tables[0].Rows[0]["pw_date2"].ToString();
txtNewCarKm.Text = ds.Tables[0].Rows[0]["pw_km"].ToString();
txtCorrBeg.Text = ds.Tables[0].Rows[0]["cor_date"].ToString();
txtCorrEnd.Text = ds.Tables[0].Rows[0]["cor_date2"].ToString();
txtCorrKm.Text = ds.Tables[0].Rows[0]["cor_km"].ToString();
txtPaintBeg.Text = ds.Tables[0].Rows[0]["pw_date"].ToString();
txtPaintEnd.Text = ds.Tables[0].Rows[0]["pw_date2"].ToString();
txtPaintKm.Text = ds.Tables[0].Rows[0]["pw_km"].ToString();
txtPorschePreBeg.Text = ds.Tables[0].Rows[0]["ppo_date"].ToString();
txtPorschePreEnd.Text = ds.Tables[0].Rows[0]["ppo_date2"].ToString();
txtPorschePreKm.Text = ds.Tables[0].Rows[0]["ppo_km"].ToString();
txtPreOwnVecBeg.Text = ds.Tables[0].Rows[0]["pov_date"].ToString();
txtPreOwnVecEnd.Text = ds.Tables[0].Rows[0]["pov_date2"].ToString();
txtPreOwnVecKm.Text = ds.Tables[0].Rows[0]["pov_km"].ToString();
txtWarrExtBeg.Text = ds.Tables[0].Rows[0]["warr_ext"].ToString();
txtWarrExtEnd.Text = ds.Tables[0].Rows[0]["warr_ext2"].ToString();
ddlSpecAgreement.SelectedItem.Text = ds.Tables[0].Rows[0]["spcl_warr"].ToString();
ddlMarketingCamp.SelectedItem.Text = ds.Tables[0].Rows[0]["spcl_cmpgn"].ToString();
txtChangeOilDate.Text = ds.Tables[0].Rows[0]["oildtlst"].ToString();
txtLastOilChange.Text = ds.Tables[0].Rows[0]["oilkmlst"].ToString();
txtInspDate.Text = ds.Tables[0].Rows[0]["svcdtlst"].ToString();
txtLastInspection.Text = ds.Tables[0].Rows[0]["svckmlst"].ToString();
txtLastReportKm.Text = ds.Tables[0].Rows[0]["mileage"].ToString();
txtExpChangeOilDate.Text = ds.Tables[0].Rows[0]["oildtnxt"].ToString();
txtNextOilChangeKm.Text = ds.Tables[0].Rows[0]["oilkmnxt"].ToString();
txtExpInspDate.Text = ds.Tables[0].Rows[0]["svcdtnxt"].ToString();
txtNextInspKm.Text = ds.Tables[0].Rows[0]["svckmnxt"].ToString();
}
My problem:When I selected random rows, the details from database are filling the textboxes but when I select another, it gives me an error of "There is no row at position 0" or my textboxes are empty (not being filled).
Appreciate your help.
Thanks...,
wish
Priti KumariRank #414 · 6K reputationPosted Jan 26, 2014, 11:01 PM EST
And See Attached Application I have to do some changes in application.
Html Markup:
onselectedindexchanged="gdvData_SelectedIndexChanged" AutoGenerateSelectButton="true">
Code:
protected void gdvData_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gdvData.SelectedRow ;
int index = row.RowIndex;
//using Below wrriten code you can get the id(primary key column value) of selected row.
//you can store primary key column value in DataKeyNames property of gridview and get this value
//using this code and then using the primary key column value you can get remaining column value from
//database.
string id = gdvData.DataKeys[index]["id"].ToString();
Getdatafromdatabase(id);
//Then populate your textbox with gridview selected row data.
txtName.Text = gdvData.Rows[index].Cells[1].Text.ToString();
txtSalary.Text = gdvData.Rows[index].Cells[2].Text.ToString();
}
Please don't forget to mark as answer if this post helps you.
Priti KumariRank #414 · 6K reputationPosted Jan 25, 2014, 7:26 AM EST
http://stackoverflow.com/questions/19983303/select-data-from-gridview-field-and-populate-textbox
http://www.aspsnippets.com/Articles/Select-GridView-Row-on-Row-Click-in-ASPNet.aspx
http://www.codeproject.com/Questions/628517/Populate-the-form-on-grid-view-link-button-click-u
wish dar23 reputationPosted Jan 25, 2014, 8:53 PM EST
Priti KumariRank #414 · 6K reputationPosted Jan 25, 2014, 10:06 PM EST
http://weblogs.asp.net/gurusarkar/archive/2010/09/22/get-gridview-rowindex-upon-button-click.aspx
And See attached aplication:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
///
/// this method data for binding grid.
///
///
private DataTable dataforbindgrid()
{
DataTable _dt = new DataTable();
_dt.Columns.Add("Name");
_dt.Columns.Add("Age");
_dt.Rows.Add("Priti", "23");
_dt.Rows.Add("priya", "24");
_dt.Rows.Add("anu", "20");
_dt.Rows.Add("sikha", "25");
return _dt;
}
private void BindGrid()
{
gdvData.DataSource = dataforbindgrid();
gdvData.DataBind();
}
///
/// this is gridview Select button Event.
///
///
///
protected void btnselect_Click(object sender, EventArgs e)
{
GridViewRow gridViewRow = (GridViewRow)(sender as Control).Parent.Parent;
//using this line find index of clickd row.
int index = gridViewRow.RowIndex;
//Then populate your textbox with gridview selected row data.
txtAge.Text = gdvData.Rows[index].Cells[1].Text.ToString();
txtName.Text = gdvData.Rows[index].Cells[2].Text.ToString();
}
}
wish dar23 reputationPosted Jan 25, 2014, 10:47 PM EST
wish dar23 reputationPosted Jan 25, 2014, 10:49 PM EST
Priti KumariRank #414 · 6K reputationPosted Jan 26, 2014, 4:49 AM EST
I have customize own code please see this code and also see attached application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Demo : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data source=PRITI-PC; initial catalog=test;uid=sa;pwd=a");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
DataTable _dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter("Select id,Name,salary from mytable", con);
ad.Fill(_dt);
gdvData.DataSource = _dt;
gdvData.DataBind();
}
///
/// this is gridview Select button Event.
///
///
///
protected void btnselect_Click(object sender, EventArgs e)
{
GridViewRow gridViewRow = (GridViewRow)(sender as Control).Parent.Parent;
//using this line find index of clickd row.
int index = gridViewRow.RowIndex;
//using Below wrriten code you can get the id(primary key column value) of selected row.
//you can store primary key column value in DataKeyNames property of gridview and get this value
//using this code and then using the primary key column value you can get remaining column value from
//database.
string id = gdvData.DataKeys[index]["id"].ToString();
Getdatafromdatabase(id);
//Then populate your textbox with gridview selected row data.
txtName.Text = gdvData.Rows[index].Cells[1].Text.ToString();
txtSalary.Text = gdvData.Rows[index].Cells[2].Text.ToString();
}
///
/// In this code i get remaining column value from database when i clicked on gridview row.
///
private void Getdatafromdatabase(string id)
{
DataTable _dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter("Select id,Name,salary,age from mytable where id='" + id + "'", con);
ad.Fill(_dt);
txtage.Text = _dt.Rows[0]["age"].ToString();
}
}
Please don't forget to mark as answer if this post helps you.
wish dar23 reputationPosted Jan 26, 2014, 10:13 PM EST
wish dar23 reputationPosted Jan 26, 2014, 10:20 PM EST
Priti KumariRank #414 · 6K reputationPosted Jan 27, 2014, 12:20 AM EST
wish dar23 reputationPosted Jan 27, 2014, 5:05 AM EST
Priti KumariRank #414 · 6K reputationPosted Jan 27, 2014, 6:59 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts