Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
This is my code file

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Details : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    Data_Acess da = new Data_Acess();
    Register rg = new Register();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            Display();
        }

    }
    private void Display()
    {
        var query = from c in db.Registers where c.Name == Convert.ToString(Session["Name"]) select c;
        GridView1.DataSource = query;
      GridView1.DataBind();
    }


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Display();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Display();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        


        int i = e.RowIndex;
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Id"]);

        //  string stname = ((TextBox)GridView1.Rows[i].Cells[0].Controls[0]).Text;
        TextBox txtname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtnameEdit");
        TextBox txtmob = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmobileEdit");
        TextBox txtAdd = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddressEdit");
        DropDownList txtcity = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlcityEdit");
        TextBox txtgender = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtgenderEdit");
        TextBox txtemail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtemailEdit");
        TextBox txtdob = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdateEdit");


     da.BindDropdownGrid(txtcity);


        Register rg = (from c in db.Registers where c.Id == ID select c).FirstOrDefault();
        rg.Name = txtname.Text;
        rg.Mobile = Convert.ToInt32(txtmob.Text);
        rg.Address = txtAdd.Text;
        rg.City = txtcity.SelectedItem.ToString();
        rg.Gender = txtgender.Text;
        rg.Email = txtemail.Text;
        rg.Dateofbirth = txtdob.Text;

        db.SubmitChanges();
        GridView1.EditIndex = -1;
        GridView1.DataBind();

        Response.Redirect("LoginForm.aspx");


    }
}


i am binding dropdown list in gridview using linq and the bining code is in data Acess layer as shown below:
C#
public void BindDropdownGrid(DropDownList ddlcityEdit)
    {
        ddlcityEdit.DataSource = from c in db.City_Names select c;
        ddlcityEdit.DataValueField = "Id";
        ddlcityEdit.DataTextField = "City";
        ddlcityEdit.DataBind();
    }
Posted
Comments
[no name] 31-Dec-14 1:04am    
Here you are using the selected Item. Rather you can use the SelectedValue or SelectedItem.Text to get the value of the dropdownlist in the txtCity control.

Clearly, your dropdownlist doesn't have the value you are trying to assign.

C#
rg.City = txtcity.SelectedItem.ToString();


Replace this line with
C#
if(txtcity.SelectedIndex>=0)
{
    //rg.City = txtcity.SelectedItem.ToString();
    //don't do this. Instead use SelectedValue property

    rg.City = txtcity.SelectedValue.ToString();
}


This will stop throwing the error. But, your job is not finished yet. You need to make sure that the you have filled the dropdownlist before this line so that there will be items to assign in this line.

In case this doesn't solve your problem please let me know :)
 
Share this answer
 
v2
Comments
Parth Mashroo 31-Dec-14 1:30am    
I have solved it with a help of my colleague i will post that soon now i have another problem that i want get previous data first in dropdown list i.e. if in my previous data i have surat as my dropdowns selected data before edit then it should get selected first!!
Hi,

You can get the dropdown list value in 2 ways.

1.
C#
ddl.SelectedItem.Text

2.
C#
ddl.SelectedValue


Use any one of them and get your problem resolved.

Thanks
Sisir Patro
 
Share this answer
 
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           if ((e.Row.RowState & DataControlRowState.Edit) > 0)
           {
               DropDownList ddlcity = (DropDownList)e.Row.FindControl("ddlcityEdit");
               da.BindDropdownGrid(ddlcity);
           }
       }
   }



i have added this code and it worked!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900