private static void Store(User user)
{
SqlConnection myConn = new SqlConnection("Integrated Security=True;initial catalog=BugTracker;data source=(local)");
myConn.Open();
string SqlQuery = "INSERT INTO User (Id, UserName, LastName, FirstName, Password, Gender, DateOfBirth, Photo) VALUES (@id, @username, @lastname, @firstname, @password, @gender, @dateofbirth, @photo)";
SqlCommand myCmd = new SqlCommand(SqlQuery, myConn);
myCmd.Parameters.Add("@id", SqlDbType.UniqueIdentifier).Value = System.Guid.NewGuid();
myCmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = user.UserName;
myCmd.Parameters.Add("@lastname", SqlDbType.NVarChar).Value = user.LastName;
myCmd.Parameters.Add("@firstname", SqlDbType.NVarChar).Value = user.FirstName;
myCmd.Parameters.Add("@password", SqlDbType.NVarChar).Value = user.Password;
myCmd.Parameters.Add("@gender", SqlDbType.Int).Value = (int)user.Gender;
myCmd.Parameters.Add("@dateofbirth", SqlDbType.DateTime).Value = user.DateOfBirth;
myCmd.Parameters.Add("@photo", SqlDbType.Image).Value = user.Photo;
myCmd.ExecuteNonQuery();
myConn.Close();
}
1 Replies - 862 Views - Last Post: 22 March 2011 - 04:10 PM
#1
Error in Writing to SQL Server database inside .NET/C#
Posted 22 March 2011 - 04:00 PM
I am trying to fill a row with these columns when my Store method is called, however, the exception I am getting has to do with adding the user.Photo. It says: "Failed to convert parameter value from a Bitmap to a Byte[]." The data type for the Photo is Image. Does this need to be a cast to a Byte[]? I've tried doing (Byte[])user.Photo
Replies To: Error in Writing to SQL Server database inside .NET/C#
#2
Re: Error in Writing to SQL Server database inside .NET/C#
Posted 22 March 2011 - 04:10 PM
Look at this tutorial's use of the System.Drawing.ImageConverter class.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|