I have 2 list boxes and 3 buttons?

Sep 4 2012 6:34 AM
HI,I have 2 list boxes and 3 buttons,1button is data moving left to right and another button moving data to right to left ,i am adding some data added to first list box to second listbox and save then the data will be saved in database,now i am remove the one listitem in second listbox and save button click its not removed the same data will be displayed

this is the stored procedure

ALTER PROCEDURE [dbo].[proc_ADD_TEST_GROUPS]
  -- Add the parameters for the stored procedure here
  @GroupName nVARCHAR(250) ,
  @GroupId nVARCHAR(20) = NULL,
  @Tests dbo.tpGroupTest READONLY
AS
BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  --SET NOCOUNT ON;
  DECLARE @Group nVARCHAR(20)
 
  BEGIN TRANSACTION trTestGroups
  IF @GroupId IS NULL
  BEGIN
    IF EXISTS (SELECT Groupname FROM tblGroups WHERE Groupname LIKE @GroupName)
    BEGIN
      RAISERROR('Group Name Already Exists',17,23)
    END
    ELSE
    BEGIN
    PRINT 'IF'
      INSERT INTO
        tblGroups (Groupname)
      VALUES
        (@GroupName)
        SELECT @Group = SCOPE_IDENTITY()
   
      INSERT INTO
        tblGroupTests (GroupId,TestId)
      SELECT
        @Group,
        TestId
      FROM
        @Tests
    END
  END
  ELSE
  BEGIN
  PRINT 'ELSE'
    INSERT INTO
      tblGroupTests (GroupId,TestId)
    SELECT
      @GroupId,
      TestId
    FROM
      @Tests T
    WHERE
      T.TestId NOT IN (SELECT TestId FROM tblGroupTests G WHERE GroupId = @GroupId AND G.TestId =T.TestId )
   
  END
  IF @@error <> 0
  BEGIN
    ROLLBACK TRANSACTION trTestGroups
  END
  ELSE
  BEGIN
    COMMIT TRANSACTION trTestGroups
  END
 
END

and c# code
-------------


  protected void ddltestcategory_SelectedIndexChanged(object sender, EventArgs e)
  {
  if (ddltestcategory.SelectedItem.Value.ToString() != "0")
  {
  System.Web.UI.HtmlControls.HtmlInputHidden userSearchCtl = (System.Web.UI.HtmlControls.HtmlInputHidden)searchGroup1.FindControl("hdnGroupId");

  if (userSearchCtl.Value.ToString() != "")
  {
  objTestGroupsDTO.GroupId = userSearchCtl.Value.ToString();
  }
  objTestGroupsDTO.CategoryId = ddltestcategory.SelectedItem.Value.ToString();
  objTestGroupsDTO.FromTo = "From";


  lbtestgroup1.DataSource = objAdminDAO.GetCategoryTests(objTestGroupsDTO);
  lbtestgroup1.DataBind();
  if (objTestGroupsDTO.GroupId != "")
  {
  DataSet ds = new DataSet();
  objTestGroupsDTO.FromTo = "To";
  ds = objAdminDAO.GetCategoryTests(objTestGroupsDTO);
 
  if (ds != null && ds.Tables.Count > 0)
  {
  lbtestgroup2.DataSource = ds.Tables[0];
  lbtestgroup2.DataBind();
  }
  else
  {
  lblMessage.Text = "No results found";
  }
  }
  }
  }
  protected void btnForward_Click(object sender, EventArgs e)
  {
  if (lbtestgroup1.Items.Count > 0)
  {
  for (int i = 0; i < lbtestgroup1.Items.Count; i++)
  {
  if (lbtestgroup1.Items[i].Selected)
  {
  //if (!lbtestgroup2.Items.Contains(lbtestgroup1.Items[i]))
  //{
  lbtestgroup2.Items.Add(lbtestgroup1.Items[i]);
  lbtestgroup1.Items.Remove(lbtestgroup1.Items[i]);
  //}
  //else
  //{
  //  lblMessage.Text = "Lab test already added";
  //}
  }
  }
 
  }
 
  }
  protected void btnBackward_Click(object sender, EventArgs e)
  {
  if (lbtestgroup2.Items.Count > 0)
  {
  for (int i = 0; i < lbtestgroup2.Items.Count; i++)
  {
  if (lbtestgroup2.Items[i].Selected)
  {
  //if (!lbtestgroup1.Items.Contains(lbtestgroup2.Items[i]))
  //{
 
  lbtestgroup1.Items.Add(lbtestgroup2.Items[i]);
  lbtestgroup2.Items.Remove(lbtestgroup2.Items[i]);
  //}
  //else
  //{
  //  lblMessage.Text = "Lab test already added";
  //}
  }
  }
  }
 
  }
  protected void SaveTestGroup(object sender, EventArgs e)
  {
  try
  {
  DataTable dtTests = TestsTable();

  DataRow drTests = dtTests.NewRow();
  System.Web.UI.HtmlControls.HtmlInputHidden userSearchCtl = (System.Web.UI.HtmlControls.HtmlInputHidden)searchGroup1.FindControl("hdnGroupId");

  TextBox txtGroupName = (TextBox)searchGroup1.FindControl("txtGroup");


  //objTestGroupsDTO.GroupName = txtGroupName.Text;
  //string strGroupID = objAdminDAO.SaveGroups(objTestGroupsDTO);
  if (userSearchCtl.Value.ToString() != "")
  {

  objTestGroupsDTO.GroupId = userSearchCtl.Value.ToString();
  }
 

  if (ddltestcategory.SelectedItem.Text.ToString() != "0")
  {
  objTestGroupsDTO.CategoryId = ddltestcategory.SelectedItem.Value.ToString();
  }
  for(int i = 0;i < lbtestgroup2.Items.Count;i++)
  {
  drTests = dtTests.NewRow();
  drTests["TestId"] = lbtestgroup2.Items[i].Value.ToString();

  //drTests["GroupId"] = userSearchCtl.Value.ToString();

 

  dtTests.Rows.Add(drTests);
  }

  string strResult = string.Empty;
 

 
  strResult = objAdminDAO.SaveGroupTests(objTestGroupsDTO, dtTests);
  lblMessage.Text = "Group test saved";
  //}
 
 
  }
  catch (Exception ex)
  {
  lblMessage.Text = ex.Message;
 
  }

  }
  protected void btnCancel_Click(object sender, EventArgs e)
  {
  Response.Redirect("~/admin/addlabtests.aspx");
  }
  public DataTable TestsTable()
  {
  DataTable dtTests = new DataTable();
  //dtTests.Columns.Add("GroupId");
  dtTests.Columns.Add("TestId");

  return dtTests;
  }



Answers (1)