开发者

Adding just Unique Data to Gridview

开发者 https://www.devze.com 2023-02-12 07:46 出处:网络
I have a table like this. Columns --> (MUSTERI, AVUKAT, HESAP (Unique)) My page design like this. Simply, first dropdown is MUSTERI, second dropdown is AVUKAT, when i click EKLE (it means ADD) but

I have a table like this. Columns --> (MUSTERI, AVUKAT, HESAP (Unique))

My page design like this.

Adding just Unique Data to Gridview

Simply, first dropdown is MUSTERI, second dropdown is AVUKAT, when i click EKLE (it means ADD) button, automaticyly getting HESAP (unique) and showing on gridview.

What i want is, if any user try to add a data which they are the same HESAP, geting an error.

For example;There is a data "2M LOJİSTİJ" "ALİ ORAL" "889" in my gridview.

Someone try to add a data like "2M LOJİSTİK" "EMRA SARINÇ" "889" showing an error and don't add to table.

My Add_Click button code is

protected void Add_Click(object sender, EventArgs e)
    {
        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(Sys开发者_如何学Ctem.Data.CommandBehavior.CloseConnection);
        Response.Redirect(Request.Url.ToString());
        myConnection.Close();
    }

And my first dropdown MUSTERI (getting HESAP auto by this field)

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

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

How can i blocking insert data which already have the same HESAP?


first check HESAP is exist or not in #AVUKAT then insert new entry.


protected void Add_Click(object sender, EventArgs e) 
    { 
        string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString; 

        SqlConnection myConnection = new SqlConnection(strConnectionString); 
        myConnection.Open(); 


        string hesap = Label1.Text.Trim(); 
        string musteriadi = DropDownList1.SelectedItem.Value; 
        string avukat = DropDownList2.SelectedItem.Value; 

        string strsql= "SELECT * FROM AVUKAT WHERE HESAP = " + hesap;
        SqlCommand com = new SqlCommand(strsql, myConnection);
        SqlDataReader dread = com.ExecuteReader(); 
        dread.read();
        if(dread.HasRows)
        {
           myConnection.Close();
           return;
        }

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

0

精彩评论

暂无评论...
验证码 换一张
取 消