Tech Off Thread

2 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

DataGrid: Edit mode, the index of a dropdownlist does not start at the right Value

Back to Forum: Tech Off
  • User profile image
    macsgirl

    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. > > 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. > > > > 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); > } > //******************************************************************** > > > marginheigh="0" marginwidth="0" > bgColor="#fef9e9"> > >

    > border="0" ms_2d_layout="TRUE"> > > > > > > > > >
    > >
    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"> > > BackColor="#FFCC66"> > BackColor="PaleGoldenrod"> > > BackColor="#954896"> > > 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'>"> > > > > > BackColor="lightblue" Font-Bold="true" Runat="server" ID="lblUserID"> > > > > > > Runat="server" ID="lblUserName"> > > > > Width="150"> > > > > > > > > > DataTextField="Store"> > > > > > > Runat="server" ID="Label2"> > > > > Width="150"> > > > > > > > > > DataSource='' > DataValueField="AccessLevelID" DataTextField="AccessLevel"> > > > > > > > > > DataTextField="UserName" DataValueField="UserID" DataSource=''> > > > > >
    > > > >

  • User profile image
    Minh

    Your code is a LITTLE hard to read, but do you have ViewState enabled?

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.