By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,928 Members | 1,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,928 IT Pros & Developers. It's quick & easy.

Bind a selected value from dropdownlist to gridview(urgent)

P: 9
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.Page
{
protected static string connect;
string ct, struName;
protected void Page_Load(object sender, EventArgs e)
{
string username = Session["UserName"].ToString();
connect = System.Configuration.ConfigurationManager.AppSetti ngs["connect"];
SqlConnection con = new SqlConnection();
con.ConnectionString = connect;
con.Open();

string s = "select PM.RegisterID,PM.PropertyID,PR.PropertyName,CD.Cat egoryName from PropertyMember PM,Property PR,CategoryDetails CD where PM.UserName='" + username + "'and PM.PropertyID=PR.PropertyID and CD.CategoryType=PM.CategoryType";
SqlDataSource1.ConnectionString = connect;
SqlDataSource1.SelectCommand = s;
con.Close();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
// DropDownList ddl1 = new DropDownList();
// //ddl1 = (DropDownList)GridView1.SelectedRow.FindControl("D ropDownList1");
// cname = ddl1.SelectedValue.ToString();
// Session["cname1"] = cname;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string username = Session["UserName"].ToString();
SqlConnection con = new SqlConnection(connect);

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

struName = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue;
e.NewValues["CategoryName"] = struName;
e.Cancel = false;
con.Close();
}
}



please help me..
thanks for all kind of support
Feb 9 '08 #1
Share this Question
Share on Google+
1 Reply


nateraaaa
Expert 100+
P: 663
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string username = Session["UserName"].ToString();
SqlConnection con = new SqlConnection(connect);

string select = "Select CategoryType from CategoryDetails where CategoryName='" + struName + "'";
SqlDataReader dr = null;
SqlCommand cmd1 = new SqlCommand(select, con);
con.Open();
dr = cmd1.ExecuteReader();
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
Feb 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.