Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my ASP.NET Application, I have a dropdownList which is used to load form Data from SQL Server. Everything was working perfect until I bind the gridView. After the gridView is bound, No postback will happen in the page.

This is my code.
C#
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
     {
     using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
     {
         con.Open();
         //using (SqlCommand com = new SqlCommand("SELECT CountryID, (CASE ISNULL(CountryLOC, '') WHEN '' THEN '' ELSE  ISNULL(CountryLOC, '') + ' - ' END) + CountryName AS CtrName FROM Country ORDER BY CountryName", con))
         using (SqlCommand com = new SqlCommand("SELECT * FROM Employee WHERE EmployeeID = " + DropDownList3.SelectedValue, con))
         {

             SqlDataReader dread = com.ExecuteReader();
             if (dread.Read())
             {
                 //main
                 firstName.Text = dread["FirstName"].ToString();
                 middleName.Text = dread["MiddleName"].ToString();
                 lastName.Text = dread["LastName"].ToString();
                 nickName.Text = dread["NickName"].ToString();
                 legalFName.Text = dread["LegalFName"].ToString();
                 legalMName.Text = dread["LegalMName"].ToString();
                 legalLName.Text = dread["LegalLName"].ToString();
                 legalMaidenName.Text = dread["LegalMaidenName"].ToString();

                 //General tab
                 dateOfBirth.Text =(dread["DateOfBirth"] != DBNull.Value ? Convert.ToDateTime(dread["DateOfBirth"].ToString()).ToString("dd/MM/yyyy") : "");
                 maritalStatus.Text = (dread["MaritalStatus"] != DBNull.Value ? dread["MaritalStatus"].ToString() : "-1");
                 genderMale.Checked = (dread["Gender"].ToString().Trim() == "M" ? true : false);
                 children.Text = dread["NumberOfChildren"].ToString();
                 homePhone.Text = dread["HomePhone"].ToString();

                 mobile1.Text = dread["Mobile1"].ToString();
                 mobile2.Text = dread["Mobile2"].ToString();
                 email.Text = dread["PersonalEmail"].ToString();
                 address1.Text = dread["AddressLine1"].ToString();
                 address2.Text = dread["AddressLine2"].ToString();
                 city.Text = dread["City"].ToString();
                 poBox.Text = dread["POBox"].ToString();
                 state.Text = dread["State_Province"].ToString();
                 zipCode.Text = dread["Zip_PostalCode"].ToString();
                 country.Text = (dread["CountryID"] != DBNull.Value ? dread["CountryID"].ToString() : "-1") ;
                 nationality.Text = (dread["Nationality_CountryID"] != DBNull.Value ? dread["Nationality_CountryID"].ToString() : "-1");

                 //Employment details tab

                 employeeID.Text = dread["EmployeeNumber"].ToString();
                 startDate.Text = ( dread["IOfficialStartDate"] != DBNull.Value ? Convert.ToDateTime(dread["IOfficialStartDate"]).ToString("dd/MM/yyyy") : "");
                 endDate.Text =  (dread["IOfficialEndDate"] != DBNull.Value ? Convert.ToDateTime(dread["IOfficialEndDate"]).ToString("dd/MM/yyyy") : "");
                 jobTitle.Text = dread["JobTitle"].ToString();
                 empLevel.Text = ( dread["EmploymentLevelID"] != DBNull.Value ? dread["EmploymentLevelID"].ToString() : "-1");
                 opLevel.Text = (dread["Default_OperationalLevelID"] != DBNull.Value ? dread["Default_OperationalLevelID"].ToString(): "-1");
                 officeLocation.Text = dread["ICubeOfficeLocation"].ToString();
                 empVisaDate.Text = ( dread["EmploymentVisaDate"] != DBNull.Value ? Convert.ToDateTime(dread["EmploymentVisaDate"]).ToString("dd/MM/yyyy") : "");
                 phoneLine.Text = dread["IDirectLineNumber"].ToString();
                 phoneExt.Text = dread["IExtensionNumber"].ToString();
                 empStatus.Text = (dread["EmploymentStatus"] != DBNull.Value ? dread["EmploymentStatus"].ToString() : "-1");
                 entity.Text = ( dread["Employee_EntityID"] != DBNull.Value ? dread["Employee_EntityID"].ToString() : "-1" );
                 reportsTo.Text = (dread["ReportsTo_EmployeeID"] != DBNull.Value ? dread["ReportsTo_EmployeeID"].ToString() : "-1");
                 dottedLineTo.Text = (dread["DottedLineTo_EmployeeID"] != DBNull.Value? dread["DottedLineTo_EmployeeID"].ToString() : "-1");
                 department.Text = (dread["DepartmentID"] != DBNull.Value? dread["DepartmentID"].ToString(): "-1");
                 organization.Text = (dread["OrganizationID"] != DBNull.Value ? dread["OrganizationID"].ToString() : "-1");
                 contractExpiryDate.Text = (dread["EmploymentContractExpiryDate"] != DBNull.Value ? Convert.ToDateTime(dread["EmploymentContractExpiryDate"]).ToString("dd/MM/yyyy") : "");
                 resVisaStartDate.Text =(dread["ResidenceVisaOfficialStartDate"] != DBNull.Value ? Convert.ToDateTime(dread["ResidenceVisaOfficialStartDate"]).ToString("dd/MM/yyyy") : "");
                 domainUserName.Text = dread["IDomainUsername"].ToString();
                 employmentType.Text = (dread["EmploymentType"] != DBNull.Value ? dread["EmploymentType"].ToString(): "-1");

                 //banking details tab

                 bankName.Text = dread["BankName"].ToString();
                 bankSwift.Text = dread["BankSWIFT"].ToString();
                 bankRoutingNumber.Text = dread["BankRoutingNumber"].ToString();
                 bankAccountNumber.Text = dread["BankAccountNumber"].ToString();
                 bankIBAN.Text = dread["BankIBAN"].ToString();

                 //IDs tab
                 laborID.Text = dread["LaborID"].ToString();
                 immigUID.Text = dread["ImmigrationUID"].ToString();
                 emiratesID.Text = dread["EmiratesIDNumber"].ToString();
                 drivingLicense.Text = dread["DrivingLicenseNumber"].ToString();
                 drivingExpiryDate.Text = (dread["DrivingLicenseExpiryDate"] != DBNull.Value ? Convert.ToDateTime(dread["DrivingLicenseExpiryDate"]).ToString("dd/MM/yyyy") : "");
                 passportNo.Text = dread["PassportNumber"].ToString();
                 passportIssueDate.Text = (dread["PassportIssueDate"] != DBNull.Value ? Convert.ToDateTime(dread["PassportIssueDate"]).ToString("dd/MM/yyyy") : "");
                 passportIssuingAuthority.Text = dread["PassportIssuingAuthority"].ToString();
                 passportExpiryDate.Text = (dread["PassportExpiryDate"] != DBNull.Value ? Convert.ToDateTime(dread["PassportExpiryDate"]).ToString("dd/MM/yyyy") : "");
                 residenceVisaFileNo.Text = dread["ResidenceVisaFileNumber"].ToString();
                 residenceVisaExpiry.Text = (dread["ResidenceExpiryDate"] != DBNull.Value ? Convert.ToDateTime(dread["ResidenceExpiryDate"]).ToString("dd/MM/yyyy") : "");
                 laborWorkPermitNo.Text = dread["LaborWorkPermitNumber"].ToString();
                 laborCardExpiry.Text = (dread["LaborCardExpiryDate"] != DBNull.Value ? Convert.ToDateTime(dread["LaborCardExpiryDate"]).ToString("dd/MM/yyyy") : "");

                 //notes tab
                 notes.Text = dread["Notes"].ToString();

                 using (SqlConnection con1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
                 {
                     con1.Open();
                     using (SqlCommand com1 = new SqlCommand("SELECT EffectiveDate, DaysOff, YearlyTickets, AverageTicketValue, PhoneAllowance FROM EmployeeBenefits WHERE EmployeeID = " + DropDownList3.SelectedValue, con1))
                     {
                         SqlDataAdapter dad = new SqlDataAdapter(com1);
                         DataTable benefitDT = new DataTable("EmployeeBenefits");
                         dad.Fill(benefitDT);
                         GridView1.DataSource = benefitDT;
                         GridView1.DataBind();
                     }
                     con1.Close();

                 }

             }
             dread.Close();
             con.Close();
         }
     }
 }
Posted
Comments
Suvendu Shekhar Giri 19-Feb-15 1:11am    
Doesn't seem to have any issue in the code. Is there any update panel?
Yesudass Moses 19-Feb-15 1:24am    
Yes, It has update panel and multiview inside it.
Yesudass Moses 19-Feb-15 1:25am    
If I remove the GridView1.Databind() it will work fine.
Yesudass Moses 19-Feb-15 1:51am    
Hi @Suvendu I got the problem.
It was my mistake. Inside GridView, I had 4 Textboxes which has same ID. That causes the issue. Thanks for your reply.

use if(!ispostback) in the pageload
 
Share this answer
 
Comments
Yesudass Moses 19-Feb-15 1:24am    
Its already used in PageLoad
Yesudass Moses 19-Feb-15 1:25am    
If I remove the GridView1.Databind() it will work fine.
Yesudass Moses 19-Feb-15 1:51am    
Hi @Kochanthu I got the problem.
It was my mistake. Inside GridView, I had 4 Textboxes which has same ID. That causes the issue. Thanks for your reply.
use like this

C#
if(!IsPostBack)
{
  Your_Function();
}
 
Share this answer
 
Comments
Yesudass Moses 19-Feb-15 1:25am    
If I remove the GridView1.Databind() it will work fine.
Yesudass Moses 19-Feb-15 1:51am    
Hi @Rashmikants I got the problem.
It was my mistake. Inside GridView, I had 4 Textboxes which has same ID. That causes the issue. Thanks for your reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900