Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Data with Same Primary Key Data in ASP.Net
    text
    copied!<p>I have a table name AVUKAT and it's columns (<code>AVUKAT</code>, <code>HESAP</code>(Primary KEY), <code>MUSTERI</code>)</p> <p>All <code>MUSTERI</code> has a one unique <code>HESAP</code> (int).</p> <p>Simple I have a page like this.</p> <p><img src="https://i.stack.imgur.com/VI8gZ.jpg" alt="enter image description here"></p> <p>First dropdown is selected MUSTERI, second is AVUKAT</p> <p>And i automaticly calculating HESAP (<code>int</code> and <code>Primary KEY</code>) with this code. (On the background.)</p> <pre><code>protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString; SqlConnection myConnection = new SqlConnection(strConnectionString); myConnection.Open(); string hesapNo = DropDownList1.SelectedItem.Value; string query = "select A.HESAP_NO from YAZ..MARDATA.S_TEKLIF A where A.MUS_K_ISIM = '" + hesapNo + "'"; SqlCommand cmd = new SqlCommand(query, myConnection); if (DropDownList1.SelectedValue != "0" &amp;&amp; DropDownList2.SelectedValue != "0") { Add.Enabled = true; Label1.Text = cmd.ExecuteScalar().ToString(); } else { Add.Enabled = false; } Label1.Visible = false; myConnection.Close(); } </code></pre> <p>I just calculating <code>HESAP</code> with this code.</p> <p>And my <code>ADD</code> button click function is;</p> <pre><code>protected void Add_Click(object sender, EventArgs e) { try { string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString; SqlConnection myConnection = new SqlConnection(strConnectionString); myConnection.Open(); string hesap = Label1.Text; string musteriadi = DropDownList1.SelectedItem.Value; string avukat = DropDownList2.SelectedItem.Value; SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection); cmd.Parameters.AddWithValue("@HESAP", hesap); cmd.Parameters.AddWithValue("@MUSTERI", musteriadi); cmd.Parameters.AddWithValue("@AVUKAT", avukat); cmd.Connection = myConnection; SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); Response.Redirect(Request.Url.ToString()); myConnection.Close(); } catch (Exception) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('Bu Müşteri Zaten Mevcut!')", true); } } </code></pre> <p>The reason use <code>try catch</code> , if anybody try to add add with same HESAP (int) value for the MUSTERI i want show an error message and don't add the table.</p> <p>But when i try to add same MUSTERI (also same HESAP) adding same MUSTERI with HESAP=0 value.</p> <p><img src="https://i.stack.imgur.com/MUZLx.jpg" alt="enter image description here"></p> <p>How can i prevent this situation? I select HESAP column is Primary KEY, but still add same MUSTERI.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload