ASP.NET - Please Read The Full Content & Post the Solution....

Asked By Venkatesh Desai on 22-Feb-12 08:48 AM

Hi All,


I am using Grid, in my grid I have footer in that footer I have
1 dropdown list with some fixed values here each item has separate ID [Value],

1 Textbox,

1 ADD Button.

Suppose In my Dropdown I have 5 Different Items with 5 Different Ids.

So my work is that I want to select one of the dropdown item & some text in Textbox & wants to INSERT that data into DB.

Here the problem is I want to add only 5 times & I didn’t want to select the repeated item in dropdown. If by mistake I select and Click on ADD button in footer it wants to show alert message. [Sorry this Item is already exists.]

So what to do????


Can any body help me???


MY CODE GOES LIKE THIS……

int



MyIAID = Convert.ToInt32(MYDROPDOWN.SelectedValue.ToString());


SqlCommand cmd = new SqlCommand("MYPROCEDURE", con);


cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@MYPARAMETER", MYVALUE);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds, "TABLENAME");

if (ds != null)

 {

  if (ds.Tables[0].Rows.Count > 0)

   {

  for (var i = 0; i < ds.Tables[0].Rows.Count; i++)

     {

     int IAID = 
        Convert.ToInt32(ds.Tables[0].Rows[i]["XXX"]);     
     
    if (IAID == MyIAID)

      {

       if (!Page.IsStartupScriptRegistered("CH"))

        {

       Page.RegisterStartupScript("CH"
        "<script>alert('This Record Is Already Exists.');</script>");

        }

      }

      else

      {

         MY INSERTION CODE GOES HERE        

      }

     }

  }

}



Thanks in Advance,

Venki.

dipa ahuja replied to Venkatesh Desai on 22-Feb-12 09:37 AM
Markup
 
<asp:UpdatePanel runat="server" ID="up1">
  <ContentTemplate>
    Username:
    <asp:TextBox runat="server" ID="Username" AutoPostBack="true" OnTextChanged="Username_Changed" />
    <div runat="server" id="UserAvailability">
    </div>
    <br />
  </ContentTemplate>
</asp:UpdatePanel>
 
Code Behind:
 
protected void Username_Changed(object sender, EventArgs e)
{
  if (!checkExist(Username.Text))
  {
    UserAvailability.InnerText = "Username taken, sorry.";     
  }
  else
  {
    UserAvailability.InnerText = "Username available!";
  }
}
 
public bool checkExist(string s)
{
  string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
   
  SqlDataAdapter da = new SqlDataAdapter("Select name from people where name='"
    + s + "'", conn);
 
  DataTable dt = new DataTable();
  da.Fill(dt);
 
  if (dt.Rows.Count > 0)
  {
    return false;
  }
  else
  {
    return true;
  }
}
 
Venkat K replied to Venkatesh Desai on 22-Feb-12 10:43 AM
If i have this requirement i usually store the values of dropdown in the session. Using Session.Add , Add the five items to the session.

Then once the Add button is clicked remove the selected item from the session and also you need to check whether the items exists in the session when clicking on add button so that you can check the items is already added or not.

Use Session.Add and Session.Remove functions to add or remove the items exist in the dropdown list. Hope this gives you an idea.