none
Unresponsive button event RRS feed

  • Question

  • Hi I dont know if I got the right sub-forum but might as well try..

    I am having some kind of a problem with my site creation , specifically creating a new item in SQL database ( Create new button , method ).

    Editing and deleting data is going without trouble, C# wise and with use of grid pan, but I really do not see whats wrong with the Add logic.

    ADD  METHOD:

     public int DodajProizvod()
        {
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = WebConfigurationManager.ConnectionStrings["proizvodi_bp"].ConnectionString;
            int dodan = 0;
          try
          {
              using (konekcija)
                {
                    konekcija.Open();
                    SqlCommand komanda = new SqlCommand("INSERT INTO Proizvodi(Ime_Proizvoda, Cijena, Rok_Trajanja, Brisanje) VALUES(@ImeProizvoda, @Cijena ,@RokTrajanja,0)", konekcija);
    
                   SqlParameter Imeparam = new SqlParameter("@ImeProizvoda", SqlDbType.NVarChar);
                    Imeparam.Value = ImeProizvoda;
                   SqlParameter CijenaParam = new SqlParameter("@Cijena", SqlDbType.NVarChar);
                    CijenaParam.Value = Cijena;
                   SqlParameter RokParam = new SqlParameter("@RokTrajanja", SqlDbType.Date);
                    RokParam.Value = RokTrajanja;
                            komanda.Parameters.Add(Imeparam);
                            komanda.Parameters.Add(CijenaParam);
                            komanda.Parameters.Add(RokParam);
                    dodan = komanda.ExecuteNonQuery();
                }                 
            }
            catch (Exception Connection_Error5)
            {
                //
            }
            return dodan;
        }

    SAVE BUTTON LOGIC ( AFTER TYPING THE DATA ):

    protected void Button1_Click(object sender, EventArgs e) { if (Request.QueryString["123"].Equals("edit")) { Proizvod proizvod = new Proizvod(); proizvod.ImeProizvoda = ImeProizvodaTextBox.Text; proizvod.Cijena = CijenaProizvodaTextBox.Text; proizvod.RokTrajanja = Convert.ToDateTime(RokTrajanjaTextBox.Text); proizvod.Id = Convert.ToInt32(polje1.Value); int klik = proizvod.Azuriranje(); if (klik == 1) { Response.Redirect("proizvodi(k).aspx"); } } else if (Request.QueryString["123"].Equals("novi")) { Proizvod proizvod = new Proizvod(); proizvod.ImeProizvoda = ImeProizvodaTextBox.Text; proizvod.Cijena = CijenaProizvodaTextBox.Text; proizvod.RokTrajanja = Convert.ToDateTime(RokTrajanjaTextBox.Text); int nov = proizvod.DodajProizvod(); if (nov == 1) { Response.Redirect("proizvodi(k).aspx"); } } }

    Any info / new control will be appreciated.

    • Moved by Kristin Xie Monday, April 25, 2016 8:11 AM ado.net related
    Sunday, April 24, 2016 6:12 PM

All replies

  • Hi,

    you should avoid empty catch blocks. If there is any exception you will not get any error message!

    When you debug the application it might help to make sure that the debugger interrupts at all thrown managed exceptions. Or you simply set a breakpoint and the go through the code step by step.

    My expectation is, that you get an exception when you try to store the data in the database.

    With kind regards,

    Konrad

    Sunday, April 24, 2016 6:41 PM
  • Ye I looked up the output after you said its about the database and it gives me the:

    Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll

    Tho I dont get it why.. I have another similar site logic(90%) and it add the data to the base fine..

    Sunday, April 24, 2016 7:02 PM
  • Hi,

    the SqlException should give you more details. Check the whole exception for details (The whole message).

    The data layer logic is also something that can be tested in a unit test easily. (Or better: should be fully tested to make sure everything works as designed!)

    With kind regards,

    Konrad

    Sunday, April 24, 2016 7:04 PM
  • Hi AdinZvonic,

    According to your code snippet, I create a demo, it seems ok, I successfully inserted a data into my database. I suppose there may be some restrictions result in data insertion fails. such as the length of data, datatype, etc

    >>Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll

    it's generic error message, I not sure what case the issue by this error message, as Konrad Neitzel said, could you please provide the detailed error message about the issue.

    Best regards,

    Cole WU

    Wednesday, May 4, 2016 7:22 AM
    Moderator