Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:55:05+00:00 2026-05-20T09:55:05+00:00

I have a table name AVUKAT and it’s columns ( AVUKAT , HESAP (Primary

  • 0

I have a table name AVUKAT and it’s columns (AVUKAT, HESAP(Primary KEY), MUSTERI)

All MUSTERI has a one unique HESAP (int).

Simple I have a page like this.

enter image description here

First dropdown is selected MUSTERI, second is AVUKAT

And i automaticly calculating HESAP (int and Primary KEY) with this code. (On the background.)

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" && DropDownList2.SelectedValue != "0")
        {
            Add.Enabled = true;
            Label1.Text = cmd.ExecuteScalar().ToString();
        }
        else
        {
            Add.Enabled = false;

        }
        Label1.Visible = false;
        myConnection.Close();
    }

I just calculating HESAP with this code.

And my ADD button click function is;

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);

        }
    }

The reason use try catch , 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.

But when i try to add same MUSTERI (also same HESAP) adding same MUSTERI with HESAP=0 value.

enter image description here

How can i prevent this situation? I select HESAP column is Primary KEY, but still add same MUSTERI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:55:06+00:00Added an answer on May 20, 2026 at 9:55 am
    1. HESAP is the primary key. However, does MUSTERI also have a Unique constraint which prevents someone from entering two MUSTERI values? That would at least prevent the data from getting into the database. So something like:

      Alter Table AVUKAT Add Constraint UC_AVUKAT Unique ( MUSTERI )

    2. Is there a CHECK constraint on HESAP which requires that the value be greater than zero? So something like:

      Alter Table AVUKAT Add Constraint CK_AVUKAT_HESAP Check ( HESAP > 0 )

      It should be noted that MySQL will ignore Check constraints. Thus, you would need to enforce this rule in a Trigger. However, many database systems such as SQL Server, Oracle, Postgres, Informix and others will enforce check constraints.

    3. I would make the following revisions

      • I would alter the query to check for whether the value exists.
      • I would incorporate the using statement to ensure that my objects were disposed.
      • I would use ExecuteNonQuery and use the number of rows returned to determine if query did not insert anything rather than implementing a global catch-all. Unless you know exactly which error you expect, you should not use Catch ( Exception ) to catch any exception no matter the type.
    protected void Add_Click(object sender, EventArgs e)
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
    
        using( SqlConnection myConnection = new SqlConnection(strConnectionString) )
        {
            myConnection.Open();
    
            string hesap = Label1.Text;
            string musteriadi = DropDownList1.SelectedItem.Value;
            string avukat = DropDownList2.SelectedItem.Value;
            string sql = @"INSERT INTO AVUKAT( MUSTERI, AVUKAT, HESAP)
                                    Select @MUSTERI, @AVUKAT, @HESAP
                                    From ( Select 1 As Value ) As Z
                                    Where Not Exists    (
                                                        Select 1
                                                        From AVUKAT As T1
                                                        Where T1.HESAP = @HESAP
                                                        )";
    
            using ( SqlCommand cmd = new SqlCommand(sql, myConnection) )
            {
                cmd.Parameters.AddWithValue("@HESAP", hesap);
                cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
                cmd.Parameters.AddWithValue("@AVUKAT", avukat);
                cmd.Connection = myConnection;
    
                int rowsAffected = cmd.ExecuteNonQuery();
    
                if ( rowsAffected = 0 )
                    // tell user that ID exists and their data couldn't be inserted.
    
                Response.Redirect(Request.Url.ToString());
                myConnection.Close();
            }
        }
    }

    When you eliminate the impossible, whatever remains, however improbable, must be the truth.

    If the HESAP value being inserted is zero, then Label1.Text must contain a zero when the Add_Click event is fired. Looking at your DropDown event handler, there are a couple of items of note.

    1. If HESAP is supposed to be an integer, you should verify that it is an integer using int.TryParse.
    2. The query should be parameterized. Even the contents of a DropDownList should be considred user input.
    3. As before, it is best to incorporate the using construct.
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int avukat;
        int hesapNo;
        bool enabled = int.TryParse( DropDownList1.SelectedItem.Value, out hesapNo ) 
            && int.TryParse( DropDownList2.SelectedItem.Value, out avukat ) 
            && hesapNo != 0 
            && avukat != 0;
    
        if ( enabled )
        {
            string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
    
            using( SqlConnection myConnection = new SqlConnection(strConnectionString) )
            {
                myConnection.Open();
    
                string query = @"Select A.HESAP_NO 
                                 From YAZ..MARDATA.S_TEKLIF A 
                                 Where A.MUS_K_ISIM = @HesapNo"
    
                using( SqlCommand cmd = new SqlCommand(query, myConnection) )
                {
                    cmd.AddParameterWithValue( "@HesapNo", hesapNo );
                    Label1.Text = cmd.ExecuteScalar().ToString();
                }
            }
        }
    
        Add.Enabled = enabled;
        Label1.Visible = false;
    }

    If you add the CHECK constraint I mentioned at the top, then the code will error on insert and the bad row will not get into the database. That should lead you back to the DataSource for DropDownList1. It would appear that its SelectedValue is being returned as zero. That would imply that source that populates DropDownList1 is pushing a value with zero in it. What is the source that populates DropDownList1?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have a table with the following columns: id - INT UNSIGNED AUTO_INCREMENT name
I have a table name Discount that has the following schema: PK DiscountID int
I have a table: ID name c_counts f_counts and I want to order all
I have a table Title Name Type ------------------------------------------------ T1 A Primary T1 B Primary
i have table with name sample in my database it has threecolumns namely words,D1,D2
I have a table with structure like that: table name: shop id_shop int(10) name
I have table with following details Table name EMPLOYEE and columns EMPID (PK smallint
i have a table name preferences under this there are multiple columns but i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.