0

I've created a website that is connected to the database, I can get data and display it on the page with no prob at all. But when I try to insert (or update) it does nothing.

I have tested the SQL query and it works just fine.

I've looked here for similar situations and questions here for the past 24 hours with no luck.

I want the website to tell if the user want a one way or two way tickets and make a request. The table has the request id which is automatically incremented then I add the id of the student who is requesting, the the id of the departure ticket then the id of return ticket (if it is 2 ways, this value can be null) there is also status which will be pending until a supervisor either accept or decline the request, once accepted, issue date will be added and status will change to approved. If declined reason will be added and status change to declined.

Main issue, when I make the request, the row is not created and added to the database for the supervisor to view later.

Here is my code:

protected void Button1_Click(object sender, EventArgs e)
{
    int parsedValue;
    int.TryParse(DropDownList1.SelectedValue, out parsedValue);
    SqlConnection myConnection = new SqlConnection(""); // I removed the connection string.
    string sqlcommand = "";
    string idString = TextBox1.Text;
    string idTwoString ="";
    bool canContune = false;
    if (parsedValue == 1)
    {
        System.Diagnostics.Debug.WriteLine("p");
        Panel3.Visible = true;
        idTwoString = TextBox2.Text;
        if (AllNumber(idString, TextBox1) && AllNumber(idTwoString, TextBox2))
        {
            canContune = true;
        }
    }
    else if (AllNumber(idString, TextBox1))
    {
        canContune = true;
    }
    if (canContune)
    {
        int dId;
        int dId2;
        int.TryParse(idString, out dId);
        int.TryParse(idTwoString, out dId2);
        sqlcommand = "INSERT INTO TicketRequest.dbo.TicketRequest (student_id, departure_id, return_id, statues, issue_date, notes) "
            + "VALUES (@student_id, @departure_id , @return_id , @statues, @issue_date, @notes)";

        try
        {
            SqlCommand cmd = new SqlCommand(sqlcommand);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = myConnection;
            myConnection.Open();
            cmd.Parameters.Add("@student_id", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("departure_id", SqlDbType.Int).Value = dId; //I used AddWithValue(@para, value) it didn't work.
            if (parsedValue == 0)
            {
                cmd.Parameters.AddWithValue("@return_id", DBNull.Value);
            }
            else
            {
                cmd.Parameters.Add("@return_id", SqlDbType.Int).Value = dId2;
            }
            cmd.Parameters.Add("@statues", SqlDbType.Text).Value = "Pending";
            cmd.Parameters.AddWithValue("@issue_date", DBNull.Value);
            cmd.Parameters.AddWithValue("@notes", DBNull.Value);
            cmd.ExecuteNonQuery();
            myConnection.Close();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }
}`

It doesn't throw any exception, I really don't know what is wrong. I would be very thankful to anyone who will point me out my mistake in Insert query. Thanks in advance.

==================================================

I apologized all, it worked just fine. it seemed that the code wasn't excuted to being with. Thanks Falanor, you helped me discover the problem. =)

9
  • 1
    Doesn't it throw an exception or does it put it under the carpet by writing it to the debug console? Commented Nov 23, 2015 at 15:53
  • nothing in the debug console at all. Commented Nov 23, 2015 at 15:56
  • as a sanity check, are you sure it's writing to the correct database, and that you are checking the same database? Commented Nov 23, 2015 at 15:57
  • The query looks ok but I don't like that try/catch pattern you have there. You have created the anti-pattern for error handling that I call try/squelch. If there is an error and you are not in debug mode there is no way to know what happened. You need to log your error and let the application know an error occurred. Commented Nov 23, 2015 at 16:01
  • 1
    Have you connected SQL Profiler to your target database? That will capture any queries sent to the server. You can view these to see if they are formed correctly, and have the data you expect. (msdn.microsoft.com/en-us/library/… - use Standard Template) Commented Nov 23, 2015 at 16:11

1 Answer 1

4

Try to check the return value.

int modified =(int)cmd.ExecuteScalar();

This is also missing the @ symbol for the parameter

cmd.Parameters.Add("departure_id", SqlDbType.Int).Value = dId; //I used AddWithValue(@para, value) it didn't work.
Sign up to request clarification or add additional context in comments.

2 Comments

It seems I forgot the @ after editing many times. and guess way there is no return value at all. seems it doesn't execute the query to begin with. will recheck my code.
Tack this on to your query to get the results. Should have included this before. SELECT CAST(scope_identity() AS int)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.