【问题标题】:Input string was not in a correct format - GUID error?输入字符串的格式不正确 - GUID 错误?
【发布时间】:2013-01-15 23:53:03
【问题描述】:

我正在使用 ASP.NET (C#),并且我有将新行插入数据库的代码。当我运行它时,我收到错误“输入字符串的格式不正确”。

我认为我已将此错误范围缩小到用户 GUID 唯一标识符,需要将其传递到插入语句中。 (从 asp:login 创建)

我知道我可以使用 Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString() 调用此用户 ID 但是我需要使用new Guid(string) 转换回Guid

这似乎对我不起作用。这是我的代码隐藏:你能发现任何错误吗?

public partial class LoggedIn_CreateDoc : System.Web.UI.Page
{
    SqlConnection sqlConn;
    SqlDataAdapter sqlDA;
    DataSet ds;
    String strConn = ConfigurationManager.ConnectionStrings["1722555Connection"].ConnectionString;

    protected void createDoc_Click(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection();

        connection.ConnectionString = ConfigurationManager.ConnectionStrings["1722555Connection"].ConnectionString;
        connection.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = connection;
        cmd.CommandTimeout = 0;

        string commandText = "INSERT INTO Documents (Name, Description, RcdID, Location, AccessLevel, DateCreated, AuthorID, DocStatus, EngStatus, QaStatus, DesignStatus) VALUES(@Name, @Description, @RCD, @Location 1, @Date, @AuthorID, 1, 1, 4, 4)";
        cmd.CommandText = commandText;
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50);
        cmd.Parameters.Add("@Description", SqlDbType.VarChar, 300);
        cmd.Parameters.Add("@RCD", SqlDbType.Int);
        cmd.Parameters.Add("@Location", SqlDbType.Int);
        cmd.Parameters.Add("@Date", SqlDbType.DateTime);
        cmd.Parameters.Add("@AuthorID", SqlDbType.UniqueIdentifier);

        cmd.Parameters["@Name"].Value = TextBoxDocTitle.Text;
        cmd.Parameters["@Description"].Value = TextBoxDocDescription.Text;
        cmd.Parameters["@RCD"].Value = ListBoxSeries.DataValueField;
        cmd.Parameters["@Location"].Value = ListBoxLocation.DataValueField;
        cmd.Parameters["@Date"].Value = DateTime.Now;
        cmd.Parameters["@AuthorID"].Value = new Guid(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());

        cmd.ExecuteNonQuery();

        connection.Close();

        Response.Write(@"<script language='javascript'>alert('Your new document has been created. \n You can now find it in your Task Panel!');</script>");

        Response.Redirect("TaskPanel.aspx");
    }
}

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 这是一个自定义会员提供商吗?你能举一个 ProviderUserKey.ToString() 输出的例子吗?顺便说一句,您的 SqlConnection 应该在 using 块中,因为您的连接不会在这里关闭(如果这只是示例代码,请道歉)
  • 这里是从 ProvideUserKey.ToString 生成的示例:“ 1f6b8431-ae07-4563-b0d2-904f0e5d2d63 ” - 我正在使用最简单的 asp:login。
  • cmd.Parameters["@AuthorID"].Value = Guid.Parse(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString()); 工作吗?

标签: c# asp.net guid


【解决方案1】:

我认为问题不在于您的ProviderUserKey。转换不会失败 - 插入会失败。

您需要将RCDLocation 转换为int 值吗?您当前正在尝试将字符串传递给 int 字段。

您应该使用ListBox.SelectedValue 从这些字段中检索选定的值,之后您可以使用Int32.TryParse 将它们转换为整数(如果您想在转换失败时抛出异常,则可以使用Int32.Parse)。

ListBox.DataValueField 不用于从ListBox 中检索选定的值,而是告诉它应该将绑定数据源中的哪个字段视为其值,并且 - 因此 - 它是 name i> 一个字段,而不是其中的值。

【讨论】:

  • 我刚想通了!!!我正在使用以下内容来获取值: ListBoxLocation.DataValueField = ds.Tables[0].Columns["LocationID"].ColumnName.ToString();现在我只需要转换为 int :)
  • 我已经更新了我的答案。当前,您正在尝试将列的名称存储为列中的值。
【解决方案2】:

您应该检查为所有 cmd.Parameters 分配了哪些值,并确保这些值与数据库中列的数据类型和限制一致。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2018-04-07
    相关资源
    最近更新 更多