2014-02-14 1 views
0

나는 두 개의 레코드가 내 데이터베이스에 복사되는 것을 막으려하고 있지만 코드를 입력 했음에도 불구하고 여전히 도움이 될만한 일이 일어나고 있습니다!누구든지이 데이터의 복사본을 입력 할 수있는 이유를 설명 할 수 있습니까?

여기 내 코드입니다 :

if (Page.IsPostBack == true) 
{ 
      SqlConnection conn = new SqlConnection("Data Source=TOSHIBA0007\\TESTSERVER;Initial Catalog=users;Integrated Security=True"); 
      conn.Open(); 
      string checkcompany = "select count(*) from Company where CompanyName = @CompanyName"; 
      SqlCommand cc = new SqlCommand(checkcompany, conn); 
      cc.Parameters.AddWithValue("@CompanyName", InputCompany.Text); 
      int temp = Convert.ToInt32(cc.ExecuteScalar().ToString()); 
      conn.Close(); 

      if (temp == 1) 
      { 
       Response.Write("Company is already registered! if this is incorrect please email us at [email protected]"); 
      } 
     } 
} 

편집

같은 이름

if (temp == 1) 

이 실패합니다 다음이 줄을 두 개 이상의 기업이있는 경우

protected void Button1_Click(object sender, EventArgs e) 
{ 
     Guid newGUID = Guid.NewGuid(); 

     SqlConnection Comp = new SqlConnection("Data Source=TOSHIBA0007\\TESTSERVER;Initial Catalog=users;Integrated Security=True"); 
     { 
      SqlCommand xp = new SqlCommand("insert into Company(GUID, CompanyName, Password, AddressLine1, AddressLine2, AddressLine3, City, PostCode, County, Country, Email, Telephonenumber, Faxnumber)Values(@ID, @CompanyName, @Password, @AddressLine1, @AddressLine2, @AddressLine3, @City, @PostCode, @County, @Country, @Email, @TelephoneNumber, @Faxnumber)",Comp); 
      xp.Parameters.AddWithValue("@ID", newGUID.ToString()); 
      xp.Parameters.AddWithValue("@CompanyName", InputCompany.Text); 
      xp.Parameters.AddWithValue("@Password", InputPassword.Text); 
      xp.Parameters.AddWithValue("@AddressLine1", InputAddress1.Text); 
      xp.Parameters.AddWithValue("@AddressLine2", InputAddress2.Text); 
      xp.Parameters.AddWithValue("@AddressLine3", InputAddress3.Text); 
      xp.Parameters.AddWithValue("@City", InputCity.Text); 
      xp.Parameters.AddWithValue("@PostCode", InputPostcode.Text); 
      xp.Parameters.AddWithValue("@County", InputCounty.Text); 
      xp.Parameters.AddWithValue("@Country", InputCountry.Text); 
      xp.Parameters.AddWithValue("@Email", InputEmail.Text); 
      xp.Parameters.AddWithValue("@TelephoneNumber", InputTelephone.Text); 
      xp.Parameters.AddWithValue("@Faxnumber", InputFax.Text); 

      Comp.Open(); 
      xp.ExecuteNonQuery(); 
      Comp.Close(); 

      if (IsPostBack) 
      { 
       Response.Redirect("Registration.aspx"); 
      } 
     } 
    } 
+0

이 코드 블록 다음에 오는 코드는 무엇입니까? – CodeCaster

+0

경쟁 조건을 피하기 위해 'CompanyName'에 고유 제한을 두어 고유 키 위반을 포착하는 것이 가장 좋습니다. –

+1

해당 회사 이름이 이미 하나 이상 있는지 확인하십시오. 당신은 count == 1을 체크하고있다. – user995219

답변

1

, temp은 2 또는 그 이상일 것입니다.

이이 시나리오에서 매우 중요하기 때문에 또한, 회사 이름 열에 고유 제한 조건을 추가하는 것을 고려

if (temp >= 1) 

와 함께이 줄을 교체합니다.

0

테스트와 버튼 클릭 사이에 링크가 표시되지 않습니다. 페이지로드시 테스트하고이 ID로 회사가 이미있는 경우 페이지에 경고를 작성합니다. 이렇게하면 버튼 클릭 코드가 실행되는 것을 멈추지 않습니다.

버튼을 클릭하여 회사를 확인하고 해당 코드가 존재하는 경우 해당 코드를 실행해야합니다.

+0

P. 실제 연결 문자열을 게시하는 것은 좋은 생각이 아닙니다. – Paddy

+0

하지만 로컬입니까? 그것은해야한다 문제가되어야한다 – CalumB

+0

그것은 중요하지 않아야한다, 아마 아마 좋은 생각이 아니다. 우리 회사는 그다지 인상 깊지 않을 것입니다. – Paddy

관련 문제