Reputation: 663
I'm using ASP.NET (C#) and I have code which inserts a new row into a database. When I run it, I get the error "Input string was not in a correct format".
I think I have narrowed this error down to the users GUID uniqueidentifier, which needs to be passed into the insert statement. (created from the asp:login)
I know I can call for this UserID by using Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString()
but then I need to convert back to a Guid using new Guid(string)
This doesn't seem to work for me. Here is my code-behind: Can you spot any errors??
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");
}
}
Upvotes: 0
Views: 2024
Reputation: 25221
I don't think that the problem is your ProviderUserKey
. The cast doesn't fail - the insert does.
Do you need to convert RCD
and Location
to int values? You are currently trying to pass strings to int fields.
You should use ListBox.SelectedValue
to retrieve the selected values from these fields, after which you can use Int32.TryParse
to convert them to integers (or Int32.Parse
if you want an exception thrown should the cast fail).
ListBox.DataValueField
is not used to retrieve the selected value from the ListBox
, but to tell it what field in the bound datasource it should treat as its value and - as such - it is the name of a field, not the value within it.
Upvotes: 2
Reputation: 15813
You should check to see what values are being assigned to all the cmd.Parameters, and make sure those values agree with the data type and restrictions of the columns in the database.
Upvotes: 0