asp.net项目C#,SQL server,Insert不能正常工作

时间:2021-07-28 01:41:58

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.

我测试了SQL查询,它运行得很好。

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

在过去的24小时里,我一直在这里寻找类似的情况和问题但没有运气。

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.

我希望网站告诉用户是否需要单向或双向票证并提出请求。该表具有自动递增的请求ID,然后我添加请求的学生的id,出发票的id然后返回票的id(如果是2种方式,此值可以为null)还有待处理的状态,直到主管接受或拒绝请求为止,一旦被接受,将添加发布日期,状态将变为已批准。如果拒绝原因并且状态更改为拒绝。

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. =)

我道歉,它工作得很好。看起来代码并没有被放弃。谢谢Falanor,你帮我发现了这个问题。 =)

1 个解决方案

#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.

#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.