1 Replies - 1097 Views - Last Post: 22 March 2011 - 04:10 PM Rate Topic: -----

#1 pkjm17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 31-July 10

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

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();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error in Writing to SQL Server database inside .NET/C#

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1