我正在使用 ASP.NET (C#),并且我有将新行插入数据库的代码。当我运行它时,我收到错误“输入字符串的格式不正确”。
我想我已经将这个错误缩小到用户 GUID 唯一标识符,它需要传递到插入语句中。(从 asp:login 创建)
我知道我可以通过 using 调用此 UserID,Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString()
但随后我需要使用转换回 Guidnew Guid(string)
这似乎对我不起作用。这是我的代码隐藏:你能发现任何错误吗?
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");
}
}