Miraj  Ahmad

Miraj Ahmad

  • NA
  • 19
  • 4k

asp.net using ajaxmodalpopExtender

Sep 6 2015 8:32 AM
i fill the dropdownlist dynamically from database then when i try to get the corresponding id of selected item from dropdown then it does not give me the id. i have a button haing click event which  contain the code below the .aspx 
 
this is my .aspx file 
<AddNewPopup:ModalPopupExtender ID="mp1" runat="server" PopupControlID="PanlAddNewPS"
TargetControlID="lbtnAddNewPS"
CancelControlID="btncanceladdNewPs" BackgroundCssClass="Background" OnLoad="mp1_Load">
</AddNewPopup:ModalPopupExtender>
<asp:Panel ID="PanlAddNewPS" runat="server" CssClass="PopupPoliceStation panel-info" align="center" Style="display: none">
<asp:UpdatePanel ID="udpInnerUpdatePanel" runat="Server" UpdateMode="Conditional">
<ContentTemplate>
<div class="panel-heading">
<div class="panel-title">Add New Police Station</div>
</div>
<br />
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<asp:Label runat="server" Text="Add District" Font-Bold="true" ID="Label2" CssClass="col-sm-4 control-label" ></asp:Label>
<div class="col-sm-8">
<asp:DropDownList runat="server" ID="ddlAddDistrict" CssClass="form-control" MaxLength="20"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="ddlAddDistrict" ErrorMessage="Select district" Font-Size="Small" ForeColor="Red" ValidationGroup="ValidateAddNewPoliceSation" InitialValue="Select New District" ></asp:RequiredFieldValidator>
</div> <br /><br />
<asp:Label runat="server" Text="Police Station" Font-Bold="true" ID="lblPSName" CssClass="col-sm-4 control-label" ></asp:Label>
<div class="col-sm-8">
<asp:TextBox runat="server" ID="txtAddPSName" CssClass="form-control" MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtAddPSName" ErrorMessage="Required" Font-Size="Small" ForeColor="Red" ValidationGroup="ValidateAddNewPoliceSation" ></asp:RequiredFieldValidator>
</div> <br /><br />
</div>
</div>
</div>
<asp:Button runat="server" CssClass="btn btn-success" ID="btnAddNewPS" Text="Add" ValidationGroup="ValidateAddNewPoliceSation" OnClick="btnAddNewPS_Click" />
<asp:Button CssClass="btn btn-info" ID="btncanceladdNewPs" runat="server" Text="Cancel" CausesValidation="false" OnClick="btncanceladdNewPs_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAddNewPS" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>--%>
 
code behind file       .aspx.cs
string MyQuery = string.Format("select DistrictId from District where DistrictName='{0}'", ddlAddDistrict.SelectedItem.Text);
SqlConnection con = DatabaseConnection.CreateConnection();
SqlCommand cmd = new SqlCommand(MyQuery, con);
string MyId;
MyId = cmd.ExecuteScalar() as string;
Response.Write("<script>alert('" + ddlAddDistrict.SelectedItem.Text + "')</script>");
Response.Write("<script>alert('" + MyId + "')</script>");
string insert = "insert into PoliceStation values('"+MyId+"','"+policeStation+"')";
InsertIntoDB.InsertQuery(insert);
if (InsertIntoDB.count > 0)
{
 Response.Write("<script>alert('Record added successfully')</script>");
 InsertIntoDB.count = 0;
}
else {
 Response.Write("<script>alert('Error: Record does not added')</script>");
}
 
it does not work at all please tell me i m being stuck in this problem and make al0t of changes still insert in the second table failed because the id which i want to pick up when the user make some selection from dropdownlist.   
 

Answers (5)