|
Hi,
show actual code (in PRE tags!) and someone will explain to you what happened.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
At a guess, it sounds like when you create your objects you are ending up with them all pointing to the same reference but without code it's hard to explain what is happening.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
sounds like u did a code by using reference type.. so that it copied 20 to another object also..But we need to examine the code
sureshkumar.S
|
|
|
|
|
Hi everyone,
I'm very new to programming and have run into a snag that I can't seem to figure out.
I have some pretty simple classes for a grading program. I have four classes (Course, Student, Assignment, StudentAssignment) StudentAssignment inherits from Assignment and adds a grade and notes.
Course has a list<assignment>
Student has a list<course> and a list<studentassignment>
When I add a course to Student list<course> I use the constructor for StudentAssignment to add a studentAssignment for every assignment in list<course>
Now, I need to keep the two in sync. What is the best way to do this. I tried foreach loops but since I can't downcast Assignment to StudentAssignment it's tough to compare the two.
I know there's probably a million ways to do it. Can anyone help?
|
|
|
|
|
Without seeing at least the basics of what you have setup it's difficult. What do you mean "keep the two in sync"? Which "two" are you referring to?
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
my original post didn't work right.
It should say course has a List<<Assignment>>
Student has a List<Course> and a List<StudentAssignment>
Since the student takes a course, all the assignments from that course should be in their Student assignment list.
The two things I need to keep in sync are the Assignment list from the course and the assignment list for the student. When I add or delete an assignment from the course assignment list, it needs to be added or deleted in the student assignment list also. If I delete a course from the student course list, I need to delete all assignments related to that course from the student list also. That's what I mean by "keep the two in sync"
|
|
|
|
|
Again Sorry.
Course has a List<Assignment>
|
|
|
|
|
Ok...that makes a bit more sense. There are several possibilities to accomplish this.
One option is to derive concrete collections for your course list and assignment list (CourseList : List<Course> and AssignmentList : List<StudentAssignment> ) and in those concrete collections override the Add/Insert/etc. methods to raise events that your other classes can then respond to and ensure that they update their appropriate fields.
A similar option would be to derive from BindingList<T>, where T is Course and StudentAssignment, and have your other classes respond to the ListChanged event.
Yet another option is to simply use List<T> like you have and either provide your own methods to do the Add/Insert/etc. and when you take an action on one list be sure that you take the appropriate action on the other list at the same time.
There are probably other ways to achieve this, but these are the ones I can think of off-hand. Each has benefits and drawbacks and one is not necessarily any better than another.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Thanks Scott, this was very helpful.
I'm now using WPF and ObservableCollection for ease of binding. I slightly modified your third suggestion and added a "Synchronizer" class and call it's add / modify / remove methods every time I add / remove / or modify any of the lists. Thanks again for the help.
|
|
|
|
|
First off, I'm guessing you didn't escape some angle-brackets (<>) in your post.
|
|
|
|
|
|
You asked this less than 12 hours ago! You should have continued on your original thread.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
ohh ooops....i couldnt find it so just thought would ask a new one..
Cheers!!
|
|
|
|
|
If you go to your profile page and click on Messages Posted you can get a list of direct links to your past 200 messages
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
thanks for the info mate....will be carefull..
|
|
|
|
|
1) The issue I am currently having is when I add the image to the sql table the it ends up giving me a exception saying "invalid parameter".
What did I do to mess this up? UpdateData() is the process of the 3 that is where the exception is comming from directly after it does update or insert the table.
2) Also if it would not be to much trouble I need help in 2 other aspects. Now that the image is at the sql table I need to be able to retrieve it directly into a picture box. There is no need to recreate the file. I have some theories but I do not know the correct way of doing this.
3) Lastly If I could get some understanding of how to access the sql server in the below method. Some if it does not make since to me to build a sql string with the parameters, then call the parameters later on in code up to update. My current methodoligy is to just build the sql string the way I want it as I go, because I can not make much since on how sql parameters work.
private void pictureBox3_DoubleClick(object sender, EventArgs e)
{
intASUserListSelectedIndex = lbxAssistSuiteUserList.SelectedIndex;
bool FoundUser = false;
FileDialog fldlg = new OpenFileDialog();
if (lbxAssistSuiteUserList.SelectedIndex > -1)
{
try
{
fldlg.InitialDirectory = @":C\";
fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif|(*.gif)|*.gif|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg|(*.bmp)|*.bmp|(*.wmf)|*.wmf|(*.png)|*.png";
if (fldlg.ShowDialog() == DialogResult.OK)
{
imagename = fldlg.FileName;
Bitmap newimg = new Bitmap(imagename);
pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox3.Image =(Image)newimg;
pictureBox3.Refresh();
lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
btnUpdateAssistUsers_Click(sender, e);
lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
updatedata();
}
fldlg = null;
lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
}
catch (System.ArgumentException ae)
{
imagename = " ";
MessageBox.Show(ae.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
private void updatedata()
{
bool FoundMe = false;
int UserIndex = -1;
try
{
if (imagename != "")
{
FileStream fs;
fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
byte[] picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
string connstr = WhatIsMyConnectionString;
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string query;
for (i = 0; i < ApplicationUsersArray.Count; i++)
{
if (((DomainUsersClass)ApplicationUsersArray[i]).WindowsLogonName == lbxAssistSuiteUserList.Items[lbxAssistSuiteUserList.SelectedIndex].ToString())
{
FoundMe = true;
break;
}
}
if (FoundMe = true)
{
if (((DomainUsersClass)ApplicationUsersArray[i]).PicIndex == "")
{
UserIndex = Convert.ToInt32(((DomainUsersClass)ApplicationUsersArray[i]).Index);
query = "insert into tblApplicationUsersPic(id_image,pic) values(@i, @p)";
}
else
{
UserIndex = Convert.ToInt32(((DomainUsersClass)ApplicationUsersArray[i]).Index);
query = "update tblApplicationUsersPic set pic = @p where id_image = @i";
}
}
else
{
MessageBox.Show("We have a serious problem. This should Never Happen. \n The Array did not find the correct users to grab their index for the Picture. \n The Application will now quit.");
return;
}
SqlCommand sc = new SqlCommand(query, conn);
sc.Parameters.AddWithValue("@i", UserIndex.ToString());
sc.Parameters.ty
sc.Parameters.AddWithValue("@p", picbyte);
sc.ExecuteNonQuery();
sc.Dispose();
conn.Close();
conn.Dispose();
if (FoundMe = true)
{
SQLUpdate(WhatIsMyConnectionString, "tblApplicationUsers", "tblApplicationUsersPic_idx = (Select idx_tblApplicationUserPic from tblApplicationUsersPic where id_image = '" + UserIndex.ToString() + "') ","delflag = 0 and idx_tblApplicationUsers = '" + UserIndex.ToString() + "' ");
}
}
}
catch (System.ArgumentException ae)
{
imagename = " ";
MessageBox.Show(ae.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
modified on Thursday, August 13, 2009 1:25 PM
|
|
|
|
|
please edit your message and surround your code with PRE tags. No one is going to read it as is.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
|
No that did not help, as you can see for yourself.
What you need is:
- no CODE tags
- PRE tags
and take care about the checkboxes below the edit box: one should NOT ignore HTML
The result could be like this:
this represents some code in PRE tags
that gets formatted, syntax colored, and
hence is readable, yielding more and better replies.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
There I think I figured it out? now it is showing in the code block.
Any Ideas what I am doing wrong. It keeps giving me a error "Invalid parameter"
modified on Thursday, August 13, 2009 1:33 PM
|
|
|
|
|
I cannot help with your main question but as far as loading the image into a picturebox, I have two suggestions.
1) I googled c# dbpicturebox and the first two hits seem extremely relevant.
2) Extending the TreeView[^] on MSDN. The code download for this article contains a DbPictureBox Control, which you may be able to adapt.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
OK I have taken you advice from the website and I am still having issues with the Invalid Parameter. What is wrong?
private void pictureBox3_DoubleClick(object sender, EventArgs e)
{
intASUserListSelectedIndex = lbxAssistSuiteUserList.SelectedIndex;
bool FoundUser = false;
FileDialog fldlg = new OpenFileDialog();
if (lbxAssistSuiteUserList.SelectedIndex > -1)
{
try
{
fldlg.InitialDirectory = @":C\";
fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif|(*.gif)|*.gif|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg|(*.bmp)|*.bmp|(*.wmf)|*.wmf|(*.png)|*.png";
if (fldlg.ShowDialog() == DialogResult.OK)
{
imagename = fldlg.FileName;
Bitmap newimg = new Bitmap(imagename);
pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox3.Image =(Image)newimg;
pictureBox3.Refresh();
lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
btnUpdateAssistUsers_Click(sender, e);
lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
updatedata();
}
fldlg = null;
lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
}
catch (System.ArgumentException ae)
{
imagename = " ";
MessageBox.Show(ae.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
private void updatedata()
{
SqlCommand cmd;
bool FoundMe = false;
int UserIndex = -1;
try
{
if (imagename != "")
{
SqlConnection cn = new SqlConnection(WhatIsMyConnectionString);
for (i = 0; i < ApplicationUsersArray.Count; i++)
{
if (((DomainUsersClass)ApplicationUsersArray[i]).WindowsLogonName == lbxAssistSuiteUserList.Items[lbxAssistSuiteUserList.SelectedIndex].ToString())
{
FoundMe = true;
break;
}
}
if (FoundMe = true)
{
if (((DomainUsersClass)ApplicationUsersArray[i]).PicIndex == "")
{
UserIndex = Convert.ToInt32(((DomainUsersClass)ApplicationUsersArray[i]).Index);
cmd = new SqlCommand("INSERT INTO tblApplicationUsersPic (id_image,BLOBData) VALUES ('" + UserIndex.ToString() + "'," + " @BLOBData)", cn);
}
else
{
UserIndex = Convert.ToInt32(((DomainUsersClass)ApplicationUsersArray[i]).Index);
cmd = new SqlCommand("update tblApplicationUsersPic set pic = @BLOBData where id_image = '" + UserIndex.ToString() + "' ", cn);
}
}
else
{
MessageBox.Show("We have a serious problem. This should Never Happen. \n The Array did not find the correct users to grab their index for the Picture. \n The Application will now quit.");
return;
}
String strBLOBFilePath = @imagename;
FileStream fsBLOBFile = new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
fsBLOBFile.Close();
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
0, 0, null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
if (FoundMe = true)
{
SQLUpdate(WhatIsMyConnectionString, "tblApplicationUsers", "tblApplicationUsersPic_idx = (Select idx_tblApplicationUserPic from tblApplicationUsersPic where id_image = '" + UserIndex.ToString() + "') ","delflag = 0 and idx_tblApplicationUsers = '" + UserIndex.ToString() + "' ");
}
}
}
catch (System.ArgumentException ae)
{
imagename = " ";
MessageBox.Show(ae.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
|
|
|
|
|
Luc has just posted a reply, so see if that helps you and come back again if not.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I have tried several different ways of adding the image to the database and the image does end up in the database but the c# application always errors saying "Parameter is invalid" I do not understand what I am doing wrong.
|
|
|
|
|
Hi,
yes that is very readable now.
disclaimer: I'm not a database specialist, I did some MySQL, a bit of SQL Server,
and no Oracle. I did save and restore images to MySQL once, that worked well.
1.
Assuming your image data will be stored as BLOB, the process indeed is:
- load the file data into a byte array
- construct and execute the SQL command that takes the byte array as a BLOB parameter
(I think your code is off in that aspect)
Suggestion: don't ever show Exception.Message, always show Exception.ToString(),
as it provides much more useful information, including line numbers.
And tell your IDE to always show line numbers in edit windows, see here.[^]
In future posts, please paste the exact exception text and indicate the line that caused it.
Maybe you need to tell the SqlCommand you are adding a BLOB value; so instead of
using AddWithValue, do an Add first (with appropriate DbType), then set the Value
of that new parameter to picByte.
2.
Once storing an image seems to work, I recommend you check your data really got stored in the database by somehow verifying the size of the record; without the image it would be a few dozen bytes; with image it should be thousands (roughly, see file size).
3.
Retrieving an image is almost identical; you don't need to create a file, as there is a Bitmap constructor that takes a byte array.
4.
In general, make Google your friend.
I tried: C# save image to oracle
and found many, maybe you'll like this one.[^]
Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|