![]() |
| ||
| DataGrid: Edit mode, the index of a dropdownlist does not start at the right Value I have two dropdownlists. I can choose say USA and then it goes into the DATA base but does not save it in the dropdownlist itself. I can not figure out why this is happening. > <code> > Edit mode the index of a dropdownlist does not start at the right value. > I have having problem when in edit mode the dropdownlist the index does not > start at the right value. I use Inner Join for my two dropdownlists. > > > > tbl_Users_StoreID > StoreID Store > 1 Name1 > 2 Name2 > > tbl_Users_AccessLevelD > AccessLevelD AccessLevel > 1 Administration > 2 Calling Card Manager > 3 Cashier > > These StoreID, AccessLevelID are in my > > tbl_Users table. > UserID > StoreID > AccessID > UserName > FullName > > Now suppose for UserID =1 > StoredID = 2 > > But when I am in editmode the Store is starting > at Name1 instead of Name2. > It is saved in the Database as StoredID =2 which corresponds to Name2. > Please help me. > > <code> > > protected void Page_Load(object sender, System.EventArgs e) > { > if (!Page.IsPostBack) > { > BindData(); > } > } > //**************************************************************** > > > > //************************************************************ > // Get Stores and populate dataset with STOREID and STORENAME > //private DataSet GetStores() > //{ > // Populate the ddlDataSet > // const string strSQLDDL = @"SELECT * FROM tbl_Users_StoreID order By > Store"; > // SqlDataAdapter myDataAdapter = new SqlDataAdapter(strSQLDDL, > myConnection); > // myDataAdapter.Fill(ddlStores, "StoreDataAptapter"); > // return ddlDataSet; > //} > //************************************************************ > > > > //************************************************************ > // Get Stores and populate dataset with ACCCESSLEVELID and ACCESSLEVEL > //private DataSet GetAccessLevels() > //{ > // Populate the ddlDataSet > // const string strSQLDDL = @"SELECT * FROM tbl_Users_AccessLevelID order > By AccessLevel"; > // SqlDataAdapter myDataAdapter = new SqlDataAdapter(strSQLDDL, > myConnection); > // myDataAdapter.Fill(ddlAccessLevels, "AccessLevelDataAptapter"); > // return ddlDataSet; > //} > //************************************************************ > > > //************************************************************ > private void DataGridUserManagementID_SelectedIndexChanged(object sender, > System.EventArgs e) > { > int intCount; > for (intCount = 1; intCount <= DataGridUserManagementID.Items.Count; > intCount ++) > DataGridUserManagementID.Items[intCount - 1].BorderStyle = > BorderStyle.Groove; > // Response.Write (DataGridUserManagementID_SelectedIndexChanged"); > //DataGridUserManagementID.SelectedItem.BorderStyle = BorderStyle.Dashed; > } > //************************************************************ > > > > //**************************************************************** > // common routine for all database updates > private void UpdateDB(string cmd) > { > myConnection.Open( ); > > // Create a SqlCommand object and assign the connection > System.Data.SqlClient.SqlCommand command = new > System.Data.SqlClient.SqlCommand( ); > command.Connection = myConnection; > command.CommandText = cmd; > // execute the sproc > command.ExecuteNonQuery( ); > } > //**************************************************************** > > > > > //**************************************************************** > private void BindData() > { > // Create the command object, passing in the SQL string > // connect to the Bugs database > string connectionString = "server=(local);uid=sa;pwd=;database=MSPOS"; > System.Data.SqlClient.SqlConnection connection = new > System.Data.SqlClient.SqlConnection(connectionString); > connection.Open(); > > > // Set the datagrid's datasource to the datareader and databind > > // get records from the Bugs table > string commandString = "SELECT tbl_Users.UserID as UserID, "; > commandString += "tbl_Users_StoreID.Store as StoreName, "; > commandString += "tbl_Users.UserName as UserName, "; > commandString += "tbl_Users.FullName as FullName, "; > commandString += "tbl_Users.ReportToID as ReportToID, "; > commandString += "tbl_Users_AccessLevelID.AccessLevel as > AccessLevelName, "; > commandString += "tbl_Users.CreationDate as CreationDate, "; > commandString += "tbl_Users.ReportToID as ReportToID, "; > commandString += "tbl_Users.LogCount as LogCount "; > commandString += "FROM (tbl_Users_AccessLevelID INNER JOIN > tbl_Users ON tbl_Users_AccessLevelID.AccessLevelID = > tbl_Users.AccessLevelID) INNER JOIN tbl_Users_StoreID ON > tbl_Users_StoreID.StoreID = tbl_Users.StoreID"; > System.Data.SqlClient.SqlCommand command = new > System.Data.SqlClient.SqlCommand(); > command.CommandText = commandString; > command.Connection = connection; > > // Create the Reader and bind it to the datagrid > SqlDataReader reader = > command.ExecuteReader(CommandBehavior.CloseConnection); > DataGridUserManagementID.DataSource=reader; > DataGridUserManagementID.DataBind(); > > } > //**************************************************************** > > > //**************************************************************** > private void DataGridUserManagementID_Edit(object source, > System.Web.UI.WebControls.DataGridCommandEventArgs e) > { > DataGridUserManagementID.EditItemIndex = e.Item.ItemIndex; > BindData(); > } > //**************************************************************** > > > //**************************************************************** > private void DataGridUserManagementID_Cancel(object source, > System.Web.UI.WebControls.DataGridCommandEventArgs e) > { > DataGridUserManagementID.EditItemIndex = -1; > BindData(); > } > //**************************************************************** > > > //************************************************************************** > **** > private void DataGridUserManagementID_ItemDataBound(object source, > System.Web.UI.WebControls.DataGridCommandEventArgs e) > { > ListItemType oType = ((ListItemType)e.Item.ItemType); > if(oType == ListItemType.Item || oType == ListItemType.AlternatingItem) > { > e.Item.Attributes.Add("onmouseover", > "this.style.backgroundColor='Silver'"); > e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='white'"); > } > > > } > //*************************************************************** > > > > > > private int ExecuteQuery(string sqlCmd) > { > // connect to the database > string connectionString = "server=(local); uid=sa;pwd=;database=MSPOS"; > System.Data.SqlClient.SqlConnection connection =new > System.Data.SqlClient.SqlConnection(connectionString); > connection.Open( ); > // call the update and rebind the datagrid > System.Data.SqlClient.SqlCommand command = new > System.Data.SqlClient.SqlCommand( ); > command.CommandText = sqlCmd; > command.Connection = connection; > return command.ExecuteNonQuery( ); > } > > //*************************************************************** > private void DataGridUserManagementID_Update(object source, > System.Web.UI.WebControls.DataGridCommandEventArgs e) > { > // Determine what store was selected > string strUserID; > string strStoreID, strStore; > string strAccessLevelID, strAccessLevel; > string strReportToID, strReportTo; > // Declare Textboxes > string strUserName, strFullName; > // Gets the value of the key field of the row being updated > strUserID = ((Label)e.Item.FindControl("lblUserID")).Text; > strStoreID = > ((DropDownList)(e.Item.FindControl("editStores"))).SelectedItem.Value; > strStore = > ((DropDownList)(e.Item.FindControl("editStores"))).SelectedItem.Text; > > strAccessLevelID = > ((DropDownList)(e.Item.FindControl("editAccessLevels"))).SelectedItem.Value; > strAccessLevel = > ((DropDownList)(e.Item.FindControl("editAccessLevels"))).SelectedItem.Text; > > strReportToID = > ((DropDownList)(e.Item.FindControl("editReportTo"))).SelectedItem.Value; > strReportTo = > ((DropDownList)(e.Item.FindControl("editReportTo"))).SelectedItem.Text; > > strUserName = ((TextBox)e.Item.FindControl("editUserName")).Text; > strFullName = ((TextBox)e.Item.FindControl("editFullName")).Text; > > // form the update statement > // connect to the database > string cmd="Update tbl_Users Set UserName='"+ strUserName +"', StoreID='"+ > strStoreID +"', FullName='"+ strFullName +"', AccessLevelID='"+ > strAccessLevelID +"', ReportToID='"+ strReportToID +"' Where UserID = "+ > strUserID +""; > ExecuteQuery(cmd); > DataGridUserManagementID.EditItemIndex = -1; > BindData( ); > > } > //************************************************************************ > > > //************************************************************************ > public void DataGridUserManagementID_Delete(object source, > System.Web.UI.WebControls.DataGridCommandEventArgs e) > { > > string cmd = "Delete from tbl_Users where UserID = " + > DataGridUserManagementID.DataKeys[e.Item.ItemIndex]; > int rowsDeleted = ExecuteQuery(cmd); > > DataGridUserManagementID.EditItemIndex = -1; > BindData( ); > } > //************************************************************************ > > > > > //************************************************************************ > > > // Given the name of a table, return a DataReader for > // all values from that table > private SqlDataReader GetValues(string tableName) > { > // connect to the database > string connectionString = "server=(local);uid=sa;pwd=;database=MSPOS"; > > // create and open the connection object > System.Data.SqlClient.SqlConnection connection = > new System.Data.SqlClient.SqlConnection(connectionString); > connection.Open( ); > > // get records from the tbl_Users table > string commandString = "Select * from " + tableName; > > // create the command object and set its > // command string and connection > System.Data.SqlClient.SqlCommand command = > new System.Data.SqlClient.SqlCommand( ); > command.CommandText = commandString; > command.Connection = connection; > > // create the DataReader and return it > return command.ExecuteReader(CommandBehavior.CloseConnection); > } > //******************************************************************** > </script> > </HEAD> > <body background="images/bg.gif" leftMargin="0" topMargin="0" > marginheigh="0" marginwidth="0" > bgColor="#fef9e9"> > <uc1:header id="Header1" runat="server"></uc1:header> > <form id="form1" runat="server"> > <TABLE height="521" cellSpacing="0" cellPadding="0" width="742" > border="0" ms_2d_layout="TRUE"> > <TR vAlign="top"> > <TD width="40" height="29"> > <!-- Include start of > header --------------------------------------------></TD> > <TD width="702" height="29"> > </TD> > </TR> > <TR vAlign="top"> > <TD height="409"></TD> > <TD><asp:datagrid id="DataGridUserManagementID" runat="server" > OnUpdateCommand="DataGridUserManagementID_Update" > OnEditCommand="DataGridUserManagementID_Edit" > OnSelectedIndexChanged="DataGridUserManagementID_SelectedIndexChanged" > AllowPaging="True" > OnPreRender="DataGridUserManagementID_SelectedIndexChanged" > AllowCustomPaging="True" > AutoGenerateColumns="False" GridLines="None" BorderStyle="Solid" > BorderColor="Black" BorderWidth="1px" > CellPadding="2" CellSpacing="2" font-names="arial" > font-size="X-Small" HorizontalAlign="Center" > Width="700px"> > <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle> > <SelectedItemStyle Font-Bold="True" ForeColor="#663399" > BackColor="#FFCC66"></SelectedItemStyle> > <AlternatingItemStyle > BackColor="PaleGoldenrod"></AlternatingItemStyle> > <ItemStyle BackColor="Beige"></ItemStyle> > <HeaderStyle Font-Bold="True" ForeColor="White" > BackColor="#954896"></HeaderStyle> > <Columns> > <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="<img > src=../images/Ok.gif border=0 align=absmiddle alt='Save changes'>" > CancelText="<img src=../images/Cancel.gif border=0 > align=absmiddle alt='Cancel editing'>" EditText="<img > src=../images/Edit.gif border=0 align=absmiddle alt='Edit this item'>"> > <ItemStyle HorizontalAlign="Center" BackColor="Yellow"></ItemStyle> > </asp:EditCommandColumn> > <asp:TemplateColumn HeaderText="ID"> > <ItemTemplate> > <asp:Label Text='<%# > Convert.ToString(DataBinder.Eval(Container.DataItem, "UserID")) %>' > BackColor="lightblue" Font-Bold="true" Runat="server" ID="lblUserID"> > </asp:Label> > </ItemTemplate> > </asp:TemplateColumn> > <asp:TemplateColumn HeaderText="User Name"> > <ItemTemplate> > <asp:Label Text='<%# > Convert.ToString(DataBinder.Eval(Container.DataItem,"UserName")) %>' > Runat="server" ID="lblUserName"> > </asp:Label> > </ItemTemplate> > <EditItemTemplate> > <asp:TextBox Runat="server" ID="editUserName" Text = '<%# > Convert.ToString(DataBinder.Eval(Container.DataItem,"UserName")) %>' > Width="150"> > </asp:TextBox> > </EditItemTemplate> > </asp:TemplateColumn> > <asp:TemplateColumn HeaderText="Store"> > <ItemTemplate> > <%# Convert.ToString(DataBinder.Eval(Container.DataItem, > "StoreName")) %> > </ItemTemplate> > <EditItemTemplate> > <asp:DropdownList id="editStores" runat="server" DataSource='<%# > GetValues("tbl_Users_StoreID") %>' DataValueField="StoreID" > DataTextField="Store"> > </asp:DropdownList> > </EditItemTemplate> > </asp:TemplateColumn> > <asp:TemplateColumn HeaderText="Full Name"> > <ItemTemplate> > <asp:Label Text='<%# > Convert.ToString(DataBinder.Eval(Container.DataItem,"FullName")) %>' > Runat="server" ID="Label2"> > </asp:Label> > </ItemTemplate> > <EditItemTemplate> > <asp:TextBox Runat="server" ID="editFullName" Text = '<%# > Convert.ToString(DataBinder.Eval(Container.DataItem,"FullName")) %>' > Width="150"> > </asp:TextBox> > </EditItemTemplate> > </asp:TemplateColumn> > <asp:TemplateColumn HeaderText="Access Level"> > <ItemTemplate> > <%# Convert.ToString(DataBinder.Eval(Container.DataItem, > "AccessLevelName")) %> > </ItemTemplate> > <EditItemTemplate> > <asp:DropdownList id=editAccessLevels runat="server" > DataSource='<%# GetValues("tbl_Users_AccessLevelID") %>' > DataValueField="AccessLevelID" DataTextField="AccessLevel"> > </asp:DropdownList> > </EditItemTemplate> > </asp:TemplateColumn> > <asp:TemplateColumn HeaderText="Reports To"> > <ItemTemplate> > <%# Convert.ToString(DataBinder.Eval(Container.DataItem, > "UserName")) %> > </ItemTemplate> > <EditItemTemplate> > <asp:DropdownList runat="server" id="editReportTo" > DataTextField="UserName" DataValueField="UserID" DataSource='<%# > GetValues("tbl_Users") %>'> > </asp:DropdownList> > </EditItemTemplate> > </asp:TemplateColumn> > </Columns> > </asp:datagrid></TD> > </TR> > </TABLE> > </form> > </body> > </HTML> > </code> |
| ||
| Re: DataGrid: Edit mode, the index of a dropdownlist does not start at the right Value Well you never seem to call UpdateDB() Function which would probably be what would store update the DB for you? Edit mode the index of a dropdownlist does not start at the right value. I have having problem when in edit mode the dropdownlist the index does not start at the right value. I use Inner Join for my two dropdownlists. tbl_Users_StoreID StoreID Store 1 Name1 2 Name2 tbl_Users_AccessLevelD AccessLevelD AccessLevel 1 Administration 2 Calling Card Manager 3 Cashier These StoreID, AccessLevelID are in my tbl_Users table. UserID StoreID AccessID UserName FullName Now suppose for UserID =1 StoredID = 2 But when I am in editmode the Store is starting at Name1 instead of Name2. It is saved in the Database as StoredID =2 which corresponds to Name2. Please help me.
Saying that you do Call ExecuteQuery() which seems to do the same thing.....redundant code..... Saying that doesn't change the problem you are having. So the data populating the Dropdownlist is not matching what the database states. I will have to examine your code more but check this article out, I think that somehow your binding is not exactly right....and that is why is does not retain the value..just cant put my finger on it. LINK to DropDownlist |
| All times are GMT -4. The time now is 11:08 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC