ASP.NET (C#) を使用しており、データベースに新しい行を挿入するコードがあります。実行すると、「入力文字列の形式が正しくありませんでした」というエラーが表示されます。
このエラーをユーザー GUID の uniqueidentifier に絞り込んだと思います。これは、insert ステートメントに渡す必要があります。(asp:login から作成)
を使用してこの UserID を呼び出すことができることはわかっていますMembership.GetUser(User.Identity.Name).ProviderUserKey.ToString()
が、使用して Guid に戻す必要があります。new 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");
}
}