用户'用户名'登录失败

时间:2009-12-28 14:34:47

标签: c# asp.net sql sqlconnection sqlexception

我有一个从MSSQL服务器中的表中选择一堆行的表单,现在,在使用表单更新数据后,此表单用于更新数据,但它重定向回到一个包含该表中所有行的数据网格的页面,并允许我选择要更新的另一行,如果我选择刚刚更新的行,我会遇到“登录失败的用户'slehan_ticketadmin'。”

现在我知道用户名/密码是正确的,因为我在一分钟前使用该表单来更新数据。我无法查看SQL错误日志,因为我的主机限制了我,但这是更新表单背后的代码。

namespace YakStudios_Support.ys_admin
{
    public partial class UpdateTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int ticketID = Convert.ToInt32(Request.QueryString["ticketID"]); // Grabs the ?ticketid number from the URL
                Ticket ticketBeingUpdated = TicketDatabase.selectTicketFromDatabase(sqlErrorLabel, ticketID); // Creates a new Ticket object to be used by the form to populate the text boxes

                /* Form Field Population */
                // Email
                emailTxt.Text = ticketBeingUpdated.getEmail();

                // Date Submitted
                dateSubText.Text = ticketBeingUpdated.getDateSubmitted().ToString();

                // Ticket Class
                classDropDown.SelectedValue = ticketBeingUpdated.getTicketClass();

                // Ticket Status
                statusDrop.SelectedValue = ticketBeingUpdated.getStatus();

                // Subject
                subjectTxt.Text = ticketBeingUpdated.getSubject();

                // Text
                textTxt.Text = ticketBeingUpdated.getTicketContent();
            }
        }

        protected void editBtn_Click(object sender, EventArgs e)
        {
            emailTxt.Enabled = true;
            dateSubText.Enabled = true;
            classDropDown.Enabled = true;
            statusDrop.Enabled = true;
            subjectTxt.Enabled = true;
            textTxt.Enabled = true;
        }

        protected void submitBtn_Click(object sender, EventArgs e)
        {
            int ticketID = Convert.ToInt32(Request.QueryString["ticketID"]); // Grabs the ?ticketid number from the URL
            DateTime convertedDate = Convert.ToDateTime(dateSubText.Text);
            Ticket ticketUpdated = new Ticket(emailTxt.Text, convertedDate, subjectTxt.Text, textTxt.Text, statusDrop.SelectedValue, classDropDown.SelectedValue);
            TicketDatabase.updateTicketInDatabase(ticketUpdated, sqlErrorLabel, ticketID);
            Response.Redirect("ticketqueue.aspx");
        }
    }
}

您看到的Ticket类只是一个类,它包含从SQL Server更新/选择/删除的票证数据,这对我来说只是一种简单的方式来以结构化的方式修改/保存数据。我想引起你的注意:

Ticket ticketBeingUpdated = TicketDatabase.selectTicketFromDatabase(sqlErrorLabel, ticketID);

我有另一个名为TicketDatabase的类,它有一堆方法可以插入/选择/更新/删除数据库中的票证。这是selectTicketFromDatabase()方法存根。

        public static Ticket selectTicketFromDatabase(Label sqlErrorLabel, int ticketID)
    {
        SqlCommand sqlCmd = new SqlCommand("SELECT * from yak_tickets");

        using (SqlConnection selSqlConn = new SqlConnection(sqlConn.ConnectionString))
        //using (sqlConn)
        {
            sqlCmd.Connection = selSqlConn;
            selSqlConn.Open(); // Open the SQL connection
            //sqlCmd.Connection = sqlConn;
            //sqlConn.Open(); // Open the SQL Connection

            SqlDataReader reader = sqlCmd.ExecuteReader();
            Ticket ticketReturning = null; // Initializes the ticketReturning but it's not allocated

            // Call during reading
            while (reader.Read())
            {
                /* Objects to hold values from reader */
                string emailString = reader["email"].ToString();
                DateTime dateSubbed = Convert.ToDateTime(reader["dateSubmitted"].ToString());
                string subjectString = reader["subject"].ToString();
                string textString = reader["ticketText"].ToString();
                string statusString = reader["statusid"].ToString();
                string classString = reader["ticketClass"].ToString();

                ticketReturning = new Ticket(emailString, dateSubbed, subjectString, textString, statusString, classString);
            }
            selSqlConn.Close();
            return ticketReturning;
        }
    }

我将在localhost服务器上对此进行测试,以查看是否是导致错误的服务器或我的代码,但我仍然愿意接受对此特定问题的建议/支持。

提前致谢!

1 个答案:

答案 0 :(得分:2)

这看起来像是与SQL登录相关的错误(与Windows身份验证无关)。

打开SSMS,尝试打开一个查询窗口:

  • “SQL Server身份验证”
  • 登录=“slehan_ticketadmin”
  • 密码=您希望密码的内容。

它失败了吗?

如果是,这里有一些选项(以另一种方式连接后):

  • 已锁定(请检查SSMS中的slehan_ticketadmin安全/用户节点)
  • 不存在(见上文)
  • 密码已更改/错误(在“安全/用户”节点中更改)
  • 默认数据库不同(应该在错误消息中告诉你)
  • SQL Server仅设置为Windows身份验证

如果没有,您的应用存储了错误的凭据

评论后编辑。

在查询窗口中,右键单击,连接,更改连接...使用上面的第一个项目符号点指令列表重新连接到同一个SQL Server实例。