wish dar

wish dar

  • NA
  • 19
  • 12.6k

populating textbox from database using gridviewrow selection

Jan 25 2014 11:12 AM
Hi,

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


Answers (12)