我不明白为什么这个SQL语句不起作用

时间:2015-03-13 14:06:34

标签: c# sql asp.net

我正在asp.net中写一个页面以获取时间表。我使用模板字段来更新GridView并将输入的信息保存到访问数据库。出于某种原因,当我运行更新命令时,它没有正确更新数据库。如果你看到任何东西,请告诉我。

RowUpdating事件的代码:

protected void table1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //get the value in the fullname textbox and assign it t a string varible
    TextBox txtFull = (TextBox)(table1.Rows[e.RowIndex].FindControl("txtfullname"));
    string FullName = txtFull.Text;
    //split the fullname string and assign the first and last name to new string variables
    //accordingly
    string[] splitstring = FullName.Split(' ');
    string firstName = splitstring[0];
    string lastName = splitstring[1];

    //assign the first and last name to the appropriate updateparameters
    SqlDataSource1.UpdateParameters["FirstName"].DefaultValue = firstName;
    SqlDataSource1.UpdateParameters["LastName"].DefaultValue = lastName;
    SqlDataSource1.UpdateParameters["FullName"].DefaultValue = FullName;

    //concantenate the username of the user's schedule you are editing.
    string username = firstName + "." + lastName;

    //instantiate an array with all the names of the updateparameters
    string[] dayArray = new string[] { "sun1","mon1","tues1","wed1","thurs1"
        ,"fri1","sat1","sun2","mon2","tues2","wed2","thurs2","fri2","sat2"};

    //loop through the controls and assign the text value of the textboxes to
    //the sqlUpdateParameters
    for (int i = 0; i < table1.Columns.Count - 4; i++)
    {
        TextBox day = (TextBox)(table1.Rows[e.RowIndex].FindControl("txt" + dayArray[i]));
        string strDay = day.Text;
        SqlDataSource1.UpdateParameters[i].DefaultValue = strDay;
    }

    string totHours = calcHours(sender, e).ToString();

    //assign the calculated total amount of hours to the totHours UpdateParameter
    SqlDataSource1.UpdateParameters["totHours"].DefaultValue = totHours;

    //set the update statement for the SqlDataSource to update the database with the 
    //information pulled from the form

    if (Convert.ToDouble(totHours) > 0)
    {
        SqlDataSource1.UpdateCommand = "UPDATE Employee SET FullName = @FullName," +
            "FirstName = @FirstName, LastName = @LastName, Sunday1 = @sun1, Monday1 = @mon1," +
            "Tuesday1 = @tues1, Wednesday1 = @wed1, Thursday1 = @thurs1, Friday1 = @fri1," +
            "Saturday1 = @sat1, Sunday2 = @sun2, Monday2 = @mon2, Tuesday2 = @tues2," +
            "Wednesday2 = @wed2, Thursday2 = @thurs2, Friday2 = @fri2, Saturday2 = @sat2," +
            "TotalHoursWorked = @totHours WHERE UserName = '" + username + "'";

        //run the update statement
        SqlDataSource1.Update();
    }
    //rebind the data in accordance with the admin

    BindData();
}

以下是浏览器中的内容之前和之后

在:

enter image description here

后:

enter image description here

更新参数的顺序从第一个星期日开始,最后4个不希望在循环中更改"table1.Columns.Count - 4"

BindData事件的代码

protected void BindData()
{
    //get the username from the cookies collection and assign it to a string varialbe
    string username = Request.Cookies["username"].Value;
    string HouseID = Request.Cookies["houseID"].Value;
    //make the sql select parameter equal to the username of the user that is logged in
    SqlDataSource1.SelectParameters["username"].DefaultValue = username;
    //check to if the user logged in is a admin. if it is then display everthing associated
    //with the houssID depending on which button was pressed, otherwise 
    //display the schedule associated with the user that is logged in.
    if (username == "admin")
    {
        if (HouseID == "5")
        {
            SqlDataSource1.SelectCommand = "SELECT * FROM Employee";
        }
        else
        {
            SqlDataSource1.SelectCommand = "SELECT * FROM Employee WHERE houseID = '" + HouseID + "'";
        }
    }
    else
    {
        SqlDataSource1.SelectCommand = "SELECT * FROM Employee WHERE UserName = '" + username + "'";
        table1.Columns[0].Visible = false;
    }
    //rebind the sqldatasource to the gridview to display the schedule correctly
    table1.DataBind();
}

如果需要其他任何内容,请告诉我,我会提供。

如果该方法出现问题,

calc hours会返回-1000。这就是它检查totHours > 0

的原因

0 个答案:

没有答案