Bind a selected value from dropdownlist to gridview(urgent)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iswar
    New Member
    • Jan 2008
    • 9

    Bind a selected value from dropdownlist to gridview(urgent)

    Hi friends..
    Im trying to bind a value from dropdownlist placed in gird view to another cell. i want to update a database field with the selected value from dropdownlist and which must be bind to gridview... please help me .. im using sql server2000 as datasource..

    public partial class Propertydetails : System.Web.UI.P age
    {
    protected static string connect;
    string ct, struName;
    protected void Page_Load(objec t sender, EventArgs e)
    {
    string username = Session["UserName"].ToString();
    connect = System.Configur ation.Configura tionManager.App Settings["connect"];
    SqlConnection con = new SqlConnection() ;
    con.ConnectionS tring = connect;
    con.Open();

    string s = "select PM.RegisterID,P M.PropertyID,PR .PropertyName,C D.CategoryName from PropertyMember PM,Property PR,CategoryDeta ils CD where PM.UserName='" + username + "'and PM.PropertyID=P R.PropertyID and CD.CategoryType =PM.CategoryTyp e";
    SqlDataSource1. ConnectionStrin g = connect;
    SqlDataSource1. SelectCommand = s;
    con.Close();
    }
    protected void DropDownList2_S electedIndexCha nged(object sender, EventArgs e)
    {
    // DropDownList ddl1 = new DropDownList();
    // //ddl1 = (DropDownList)G ridView1.Select edRow.FindContr ol("DropDownLis t1");
    // cname = ddl1.SelectedVa lue.ToString();
    // Session["cname1"] = cname;
    }
    protected void GridView1_RowUp dating(object sender, GridViewUpdateE ventArgs e)
    {
    string username = Session["UserName"].ToString();
    SqlConnection con = new SqlConnection(c onnect);

    string select = "Select CategoryType from CategoryDetails where CategoryName='" + struName + "'";
    SqlDataReader dr = null;
    SqlCommand cmd1 = new SqlCommand(sele ct, con);
    con.Open();
    dr = cmd1.ExecuteRea der();
    while (dr.Read())
    {
    struName = dr[0].ToString();
    }

    struName = ((DropDownList) ((GridView)send er).Rows[e.RowIndex].FindControl("D ropDownList1")) .SelectedValue;
    e.NewValues["CategoryNa me"] = struName;
    e.Cancel = false;
    con.Close();
    }
    }



    please help me..
    thanks for all kind of support
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    protected void GridView1_RowUp dating(object sender, GridViewUpdateE ventArgs e)
    {
    string username = Session["UserName"].ToString();
    SqlConnection con = new SqlConnection(c onnect);

    string select = "Select CategoryType from CategoryDetails where CategoryName='" + struName + "'";
    SqlDataReader dr = null;
    SqlCommand cmd1 = new SqlCommand(sele ct, con);
    con.Open();
    dr = cmd1.ExecuteRea der();
    while (dr.Read())
    {
    struName = dr[0].ToString();
    }
    Where is struName ever set? You have a select query where CategoryName is suppose to = struName but I never see struName set. The problem may be your query does not return anything so the dr.Read never has any rows making struName always = string.Empty or "". Have to debugged your code to verify that the code drops into your while loop and sets struName to a value?

    Nathan

    Comment

    Working...