Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In a C# silverlight application I've got a datagrid control that gets contact details from an sql database table. I can add new details to the datagrid by typing in the textboxes and clicking the add button. there is an upload button to upload an image in the Image control and works fine. but I can't seem to save an image for each contact in the table and veiw it along with the other information using wcf service.

here is what I've wrote in my upload image button:

private void uploadbtn_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG files|*.jpg";
//openFileDialog.Filter = "Images (*.jpg, *.png, *.bmp)|*.jpg;*.png;*.bmp";
if (openFileDialog.ShowDialog() == true)
{
Stream stream = (Stream)openFileDialog.File.OpenRead();
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
BitmapImage bi = new BitmapImage();
bi.SetSource(stream);
MyImage.Source = bi;
string fileName = openFileDialog.File.Name;

}
}

in my add contact button I have :

ServiceReference1.Contact contact = new ServiceReference1.Contact();
ServiceReference1.Service1Client connection = new ServiceReference1.Service1Client();
connection.InsertContactCompleted += new EventHandler<servicereference1.insertcontactcompletedeventargs>(connection_InsertContactCompleted);
contact.ConName = txtname.Text;
contact.ConNumber = txtnumber.Text;
contact.ConDescription = txtdescription.Text;


and in the service :

[OperationContract]
public int InsertContact(Contact cnt)
{
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = " insert into Contact(ConName, ConNumber,ConDescription) values(@1,@2,@3)";
cmd.CommandTimeout = 300;
cmd.Parameters.Add("@1", System.Data.SqlDbType.VarChar, 50).Value = cnt.ConName;
cmd.Parameters.Add("@2", System.Data.SqlDbType.VarChar, 50).Value = cnt.ConNumber;
cmd.Parameters.Add("@3", System.Data.SqlDbType.VarChar, 50).Value = cnt.ConDescription;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return i;
}
else
{
return 0;
}
}
}
}
I don't know how to add the image to these codes.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900