简体   繁体   English

asp.net项目C#,SQL Server,插入不起作用

[英]asp.net project C#, SQL server, Insert is not working

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. 该表具有自动增加的请求ID,然后添加正在请求的学生的ID,出发票的ID,然后是回程票的ID(如果是2种方式,则该值为null)有在主管接受或拒绝请求之前,状态将一直待定,一旦接受,将添加发布日期,状态将更改为已批准。 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. =)

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM