I'm allowing users to upload a file and give it a name. Right now, I'm just adding a row to a database with the following:
Username, DataSetID (GUID), DataSetName, TimeStamp
.
I'm getting the error
"Object must implement IConvertible."
Guid unique = Guid.NewGuid();
var current = DateTime.Now;
string sql = "INSERT INTO datasets (userid, datasetid, datasetname, timestamp) VALUES (@userid, @datasetid, @datasetname, @timestamp);";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
using (SqlCommand command = new SqlCommand(sql, con))
{
command.Parameters.Add("@userid", SqlDbType.Text);
command.Parameters["@userid"].Value = System.Web.HttpContext.Current.User.Identity.GetUserId();
command.Parameters.Add("@datasetid", SqlDbType.Text);
command.Parameters["@datasetid"].Value = unique;
command.Parameters.Add("@datasetname", SqlDbType.Text);
command.Parameters["@datasetname"].Value = datasetname.ToString();
command.Parameters.Add("@timestamp", SqlDbType.DateTime);
command.Parameters["@timestamp"].Value = current;
con.Open();
command.ExecuteNonQuery();
con.Close();
}
}
You can't implicitly convert a Guid (unique
in your code) to a string.
Doing so explicitly:
command.Parameters["@datasetid"].Value = unique.ToString();
should fix it.
See https://msdn.microsoft.com/en-us/library/system.guid(v=vs.110).aspx for documentation. As you can see, Guid
does not implement the IConvertible
interface which would allow for implicit conversion.