|
Is there a good N-Tier book for Newbies in C# and SqlServer?
Allan F. Gagnon
|
|
|
|
|
I made a panel with a gradient as background (LinearGradientBrush) but the gradient is sequential and err...high quality, i want to make a gradient like the one in the panel in CCleaner where you can see like blocks, how can i do it?
|
|
|
|
|
I think the simplest method would be to fill a number of rectangles and vary the colour yourself. I couldn't find a screenshot of CCleaner with a segmented gradient but here is some code that I think does what your after.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
int segments = 10;
Color start = Color.FromArgb(81, 165, 244);
Color end = Color.FromArgb(54, 102, 147);
RectangleF area = ClientRectangle;
area.Height /= segments;
float r = start.R, g = start.G, b = start.B;
float dr = (end.R - r) / (segments - 1), dg = (end.G - g) / (segments - 1), db = (end.B - b) / (segments - 1);
using (SolidBrush brush = new SolidBrush(start))
{
for (int i = 0; i < segments; i++)
{
brush.Color = Color.FromArgb((byte)r, (byte)g, (byte)b);
e.Graphics.FillRectangle(brush, area);
area.Y += area.Height;
r += dr;
g += dg;
b += db;
}
}
}
|
|
|
|
|
Hi,
I am trying to display an image from the sql 2000 database on to the picture box control on windows app (VS 2.0).
I am able to pull the image in to byte array. I also am able to put the byte array in to the Memory stream by writing it like stream.Write(image, 0, image.Length)
However, when i try to create a bitmap out of the memory stream it thorows an exception stating "parameter is not valid".
The second approach I tried was to use Image.FromStream(ms1) method, but it also says "parameter is not valid" when it tries to get the image from stream (memory stream).
The third approach i tried was writing to a temporary file stream and then read from file using Image.FromFile(strfn, true). This line gets an error stating "Out of memory".
Nothing seems to be working. Please shed some light if possible. All i want to do is display an image from sql db field(image field) on to picture box in windows application using C# 2.0
Please look at the code below for detail. The line in bold where I am getting my exceptions.
// Put user code to initialize the page here
MemoryStream stream = new MemoryStream();
SqlConnection connection = new SqlConnection(@"my connection string");
try
{
connection.Open();
SqlCommand command = new SqlCommand("select image from images Where EntryDate > '2/15/2007'", connection);
byte[] image = (byte[])command.ExecuteScalar();
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
/////testing 2
MemoryStream ms1 = new MemoryStream(image);
exceptionPictureBox.Image = Image.FromStream(ms1);
///testing 3
string strfn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
fs.Write(image, 0, image.Length);
fs.Flush();
fs.Close();
exceptionPictureBox.Image = Image.FromFile(strfn, true);
/////testing 3
}
finally
{
connection.Close();
stream.Close();
}
Thanks
Needy
|
|
|
|
|
I have an image of a checkerboard that I am using to calibrate a camera. I wanted to show a grid on top as a green overlay and the points of intersection as blue dots (squares). Right now I have a filter that goes over the image and creates another bitmap with this information on a black background. The grid lines are two pixels wide and the points are 2pixels x 2pixels. Eventually I would like to add some type of zoom functionality to the Picturebox showing the image.
Having said all of this, is it better to:
1) Add the images together (using OR) realizing that the colors would look lighter on the white squares because of the Red channel. This could be fixed by ANDing the Red channel.
2) Adding the two images using black as the transparency color for the overlay image. Although, I'm not sure how to do this with Bitmaps objects.
3) Use GDI+. I've never used this to know how difficult this may be. I think it would look better because the vector graphics could be make to look the same at every scale.
Thanks,
Brad
The secret to creativity is knowing how to hide your sources. - Albert Einstein
|
|
|
|
|
Hi,
I'm not sure I understand what you want to achieve, but maybe this helps:
- you can have one image in say a Bitmap, and create a Graphics object that
allows you to draw onto it; use Graphics.FromImage(image)
- now you can modify your image with graphics methods such as DrawLine and DrawString.
All the time you only have one image, which you can then scale, display and save
any way you like.
Hope this helps.
Luc Pattyn
|
|
|
|
|
Hello,
I am trying to speed up the update of a DataTable. The short story is that I have a DataTable 'A' that has approximately 10-15 columns of information, depending on the data loaded. I have a couple of extra columns that get added to the table at runtime and I need to populate those columns based on the values in another table, 'B', that has 3 columns: an ItemID column for matching the ItemId column in table A and two other columns. I could have as many as 10K rows in DataTable A and the number of rows in DataTable B could vary. It is possible, too, that DataTable B may have rows where the ItemId doesn't match any ItemId in DataTable A, and it those instances, I want to ignore the rows.
My current solution involves something like this:
DataColumn aColNew1 = tableA.Columns["New1"];
DataColumn aColNew2 = tableA.Columns["New2"];
DataColumn bColNew1 = tableB.Columns["New1"];
DataColumn bColNew2 = tableB.Columns["New2"];
DataColumn bColItemId = tableB.Columns["ItemId"];
DataRow[] bRows = tableB.Select();
for (int i = 0; i < bRows.Length; ++i)
{
DataRow[] aRows = tableA.Select("ItemId = '" + bRows[i][bColID].ToString() + "'");
if (aRows.Length > 0)
{
aRows[0][aColNew1] = bRows[i][bColNew1];
aRows[0][aColNew2] = bRows[i][bColNew2];
}
}
Now, I'm am pretty new to C#, so this might not be the most efficient way to do this. I hope it isn't, because it is taking a considerable amount of time (close to 40 seconds) to execute this when tables A and B have around 10K rows.
Any suggestions on how I can speed this up would be much appreciated.
Thanks,
Matt
|
|
|
|
|
The Select() calls were killing me.
It was actually faster to just select everything, sorted by ItemId and iterate through the two arrays using a while-loop nested within a for-loop, bailing out of the while whenever I got a match or when the Id I was comparing was greater than the current one in the while loop.
Ended up cutting down the processing time to maybe a second or so. Should have realized how inefficient the original algorithm was.
|
|
|
|
|
I am having a lot of trouble finding sample code on the internet showing how to browse for a folder/directory through the propertygrid control.
I would like a user to be able to select a folder as backup directory for a project using the PropertyGrid control instead of using a standard windows form UI.
Currently my code looks like this:
<br />
using System.Windows.Forms.Design;<br />
using System.Drawing.Design;<br />
<br />
[global::System.Configuration.UserScopedSettingAttribute()]<br />
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]<br />
[EditorAttribute(typeof(FileNamEditor), typeof( UITypeEditor ))]<br />
[CategoryAttribute("Backup Directories")]<br />
[DescriptionAttribute("Default backup directory")]<br />
<br />
public string BackupDirectory <br />
{<br />
get <br />
{<br />
return _backupDirectory;<br />
}<br />
set <br />
{<br />
_backupDirectory = value;<br />
}<br />
}<br />
|
|
|
|
|
hi
change this line of code
[EditorAttribute(typeof(FileNamEditor), typeof( UITypeEditor ))]
with
[EditorAttribute(typeof(FolderNamEditor), typeof( UITypeEditor ))]
regards
|
|
|
|
|
Thank you for the quick reply, however, that is not working either. Visual Studio keeps giving me a compile error that I am missing a reference or using directive in regards to "FileNameEditor" or "FolderNameEditor". I just can't get it to work.
|
|
|
|
|
I solved the problem. You have to add a reference using the "add reference" dialog box to System.Design, then it works. If you don't make this reference then it will not work.
|
|
|
|
|
I want to make a Login class that I pass info from a text box in to a set variable in the class and then use a get variable to test if my login was successfull. Is this possible?
Newbie at C# but a big wannabe
Allan F. Gagnon
|
|
|
|
|
Yes
only two letters away from being an asset
|
|
|
|
|
What is a "set variable"? What is a "get variable"?
|
|
|
|
|
This is my question!!
Allan F. Gagnon
|
|
|
|
|
Guess I was assuming he meant set/get, but you never know around here.
only two letters away from being an asset
|
|
|
|
|
allan.gagnon wrote: I want to make a Login class that I pass info from a text box in to a set variable in the class and then use a get variable to test if my login was successfull. Is this possible?
I think there is some confusion about various terms because to me your request didn't make a lot of sense.
A class can have properties. A property can have a getter and/or setter and it could look like this:
class MyClass
{
private string someField;
public string SomeProperty
{
get
{
return this.someField;
}
set
{
this.someField = value;
}
}
}
As you can see the getter and setter refer to the same member variable (field). Or it can do other work to simulate similar functionality. Essentially a getter and setter are special methods that C# dresses up in syntactic sugar to make it look like a variable.
If you were to make a Login class, you might have a method that takes a user name and password. You can then use a property to tell the outside world whether the Login was successful.
class Login
{
private bool isLoggedIn;
public void AuthenticateUser(string userName, string password)
{
}
public bool IsLoggedIn
{
get
{
return this.isLoggedIn;
}
}
}
Does this help? Or does it help you form a better idea so that you can rephrase your question?
|
|
|
|
|
Great
Thank You!!
Allan F. Gagnon
|
|
|
|
|
Ok Thank you for the help so far this time I will try to explain the question better. I guess I am a newbie at c# and a newbie at asking questions about c#.
Here is question number 2:
I have wrote this code
public static void Login(String LoginBox)
{
string strConn, strSQL;
bool LoginSucceeded;
strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\admin\My Documents\Visual Studio 2005\Projects\GasCabinetSimulator\GasCabinetSimulator\GasCabinet.mdf;Integrated Security=True;User Instance=True";
strSQL = "SELECT UserID, PassWord, IsLoggedIn FROM tblLogin";
using (SqlConnection cn = new SqlConnection(strConn))
{
try
{
cn.Open();
}
catch (SqlException ex)
{
MessageBox.Show("Connect Attempt Failed");
MessageBox.Show(ex.Message);
return;
}
using (SqlCommand cmd = new SqlCommand(strSQL, cn))
{
try
{
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
if (LoginBox == rdr["PassWord"].ToString())
{
MessageBox.Show("Login successful","Login Success",
MessageBoxButtons.OK,MessageBoxIcon.Information);
MessageBox.Show(rdr["IsLoggedIn"].ToString());
rdr["IsLoggedIn"] = true;
}
rdr.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show("Query failed");
MessageBox.Show(ex.Message);
return;
}
}
cn.Close();
However I am getting this error message when I build my project:
Error 1 Property or indexer 'System.Data.Common.DbDataReader.this[string]' cannot be assigned to -- it is read only C:\Documents and Settings\admin\My Documents\Visual Studio 2005\Projects\GasCabinetSimulator\GasCabinetSimulator\ClassLogin.cs 46 37 GasCabinetSimulator
This line is the error: rdr["IsLoggedIn"] = true;
Where am i going wrong?
Thank you again for all your help!!
Allan F. Gagnon
|
|
|
|
|
allan.gagnon wrote: However I am getting this error message when I build my project:
Error 1 Property or indexer 'System.Data.Common.DbDataReader.this[string]' cannot be assigned to -- it is read only C:\Documents and Settings\admin\My Documents\Visual Studio 2005\Projects\GasCabinetSimulator\GasCabinetSimulator\ClassLogin.cs 46 37 GasCabinetSimulator
This line is the error: rdr["IsLoggedIn"] = true;
The clue is in the name. You have a data reader. You cannot use it to write values to the database.
In order to update the database you need to issue a separate SQL command.
UPDATE tblLogin SET IsLoggedIn = 1 WHERE UserID = ????
You may wish to rethink your logic somewhat. Because you are not matching a user ID to a password. As soon as someone passes any valid password for any account (it doesn't matter which) they are permitted access. Who is accessing in reality and which account is being used is anyone's guess.
A Security tip: Don't let passwords escape from the database. The less you pass password data around the safer your system is. Consider:
SELECT COUNT(*) FROM tblLogin WHERE UserID = @userID AND Password = @password
And you can use ExecuteScalar to get a single value back. 0 = no match, 1 = match, any other value means that the data is invalid.
Also, I've used parameters here: @userID and @password
You use the Parameters collection of the SqlCommand to pass them in.
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT COUNT(*) FROM tblLogin "+
"WHERE UserID = @userID AND Password = @password";
cmd.Connection = cn;
cmd.Parameters.Add("@userId", theUserId);
cmd.Parameters.Add("@password", thePassword);
int matches = (int)cmd.ExecuteScalar();
if (matches == 0)
{
}
else
{
}
Does this help?
|
|
|
|
|
This is completely awesome I have been trying to figure something like this out for about two weeks in books training videos and all with no luck!!! This is a great answer and I completely understand what your point is. Thank You very much.
This website is the greatest!!!
;P
Allan F. Gagnon
|
|
|
|
|
hi all,
how can i make an aura on image on the stage
Generator
|
|
|
|
|
SqlDataReader implements IDisposable in .NET 1.1, yet its Dispose method is private. How can this be? If I create an interface with a single member and try to implement it as private, it obviously won't compile!
This isn't causing me a huge problem, but it doesn't make sense and I'd like to know why.
Note that, in .NET 2, SqlDataReader inherits from DbDataReader, which has a public Dispose method.
David.
|
|
|
|
|
Hi..
No it's not private.
You can Implement an Interface in 2 ways.
First:
<br />
public class Blah:IDisposable<br />
{<br />
public void Dispose()<br />
{<br />
}<br />
}<br />
in This case you can say:
<br />
myBlahObject.Dispose();<br />
But, when you Implement it that way:
<br />
public class Blah:IDisposable<br />
{<br />
void IDisposable.Dispose()<br />
{<br />
}<br />
}<br />
<br />
myBlahObject.Dispose();<br />
will not work. but if you say:
<br />
(myBlahObject as IDisposable).Dispose();<br />
it works fine
hope this helps
greets
m@u
|
|
|
|