|
I guess I was on the right track. Here's the code that makes it work.
BindingSource _mainBS;<br />
string _fieldname;<br />
TARData.TARDataSet.DataRow[] _mRA;<br />
string _searchstring;<br />
int _currentPosition;
_mainBS is assigned by a constructor, likewise _fieldname .
public void FindNext() {<br />
if (_searchstring != textBox1.Text)<br />
{<br />
_searchstring = textBox1.Text;<br />
_currentPosition = 0;<br />
string searchCrit = getColumnSearchString(((DataRowView)_mainBS.Current).DataView.Table.Columns[_fieldname].DataType);<br />
_mRA = (TARData.TARDataSet.Carter_TAR_MainRow[])((DataRowView)_mainBS.Current).DataView.Table.Select(<br />
searchCrit + " LIKE '%" + _searchstring + "%'");<br />
}<br />
<br />
if (_mRA.Length > 0) {<br />
this._mainBS.Position = this._mainBS.Find("Main_ID", _mRA[_currentPosition]["Main_ID"]); ;<br />
}<br />
_currentPosition = (_currentPosition + 1) % _mRA.Length;<br />
}
Just in case _fieldname is an int type, I throw in this little tidbit to perform a convert.
private string getColumnSearchString(Type type)<br />
{<br />
if (type == typeof(int))<br />
return "Convert(" + _fieldname + ",System.String)";<br />
else return _fieldname;<br />
}
Not too efficient, but I still like admiring it. Not a bad way to spend an hour of my day.
|
|
|
|
|
Is there anyway to unbox an object without knowing its exact type? I have a situation where I have an object that is guaranteed to be castable to a double. I want to compare it to another value that I have stored as a double. Just casting the object to a double results in an invalid cast exception if the object is not of type double.
The only other way I can see to do this is to hard code cases for every value type that the object could possibly be, but this is obviously a pretty undesirable solution.
|
|
|
|
|
You could always try
try{
Convert.ToDouble(...)
}
catch (...)
{
} If you're using .NET1.1. If you're using .NET2 then take a look at
bool isDouble = double.TryParse(value.ToString(), out doubleVal);
|
|
|
|
|
Thanks for the advice. Parsing it as a string works perfectly.
|
|
|
|
|
How can this
jka347 wrote: Just casting the object to a double results in an invalid cast exception if the object is not of type double
be a problem if this
<blockquote class="FQ"><div class="FQA">jka347 wrote:</div>an object that is guaranteed to be castable to a double</blockquote>
is true?
You could also try object.Equals ( val1 , val2 )
|
|
|
|
|
Hey guys, I have this really weird issues and I can't seem to figure it out, the code list below run fine with no errors what so ever when I run it from a GUI application, but when i put it into a service and use it, it gives me a System.NullException error, but what is weird is that it gives me that error in the Finally Statement, but for some reason it doesn't even bother writing to the registry like it is suppose to, but it keep in mind the exact same code works if it is in a GUI application.
I have labeled the two lines that the error is given at.
Thanks for those who help.
Travis
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Data.SqlClient;<br />
using System.Data;<br />
using Microsoft.Win32;<br />
<br />
class VCS<br />
{<br />
private RegistryKey regKey;<br />
private SqlDataReader rdr = null;<br />
private SqlCommand cmd = null;<br />
private string procValue;<br />
private string connString<br />
<br />
private SqlConnection virasecConn = new SqlConnection();<br />
<br />
public void executeStorProc(string procedureName)<br />
{<br />
try<br />
{<br />
openConn();<br />
<br />
cmd = new SqlCommand(procedureName, virasecConn);<br />
cmd.CommandType = CommandType.StoredProcedure;<br />
<br />
rdr = cmd.ExecuteReader();<br />
<br />
while (rdr.Read())<br />
{<br />
regKey = Registry.LocalMachine.OpenSubKey<br />
("SOFTWARE\\VIRASEC, LLC\\VCS", true);<br />
<br />
procValue = rdr[0].ToString();<br />
<br />
regKey.SetValue("Test", procValue);<br />
}<br />
}<br />
catch (Exception ex)<br />
{<br />
Console.WriteLine(ex.Source + "\n" + ex.StackTrace);<br />
}<br />
finally<br />
{<br />
regKey.Close(); - Error occurs here <br />
rdr.Close(); - Error occurs here<br />
closeConn(); <br />
} <br />
}<br />
<br />
public void openConn()<br />
{<br />
virasecConn.ConnectionString = connString;<br />
<br />
try<br />
{<br />
virasecConn.Open();<br />
}<br />
catch (Exception ex)<br />
{<br />
Console.WriteLine(ex.Source + "\n" + ex.StackTrace);<br />
}<br />
}<br />
<br />
public void closeConn()<br />
{<br />
try<br />
{<br />
virasecConn.Close();<br />
}<br />
catch (Exception ex)<br />
{<br />
Console.WriteLine(ex.Source + "\n" + ex.StackTrace);<br />
}<br />
}<br />
}<br />
<br />
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Diagnostics;<br />
using System.ServiceProcess;<br />
using System.Text;<br />
using System.Threading;<br />
<br />
<br />
public partial class VCS_Service : ServiceBase<br />
{<br />
Thread threadOne = new Thread(new ThreadStart(mainThread)); <br />
static VCS myVCS = new VCS();<br />
<br />
public VCS_Service()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
protected override void OnStart(string[] args)<br />
{<br />
threadOne.Start();<br />
}<br />
<br />
protected override void OnStop()<br />
{<br />
threadOne.Abort();<br />
}<br />
<br />
public static void mainThread()<br />
{<br />
while (true)<br />
{<br />
myVCS.executeStorProc("GetWesHomeNumber");<br />
Thread.Sleep(300000);<br />
}<br />
}<br />
<br />
}
|
|
|
|
|
Hi,
your code is not safe.
regKey initially is null, it is only some way down the try block it may get a non-null
value, yet you use it without any checking in the finally block.
Imagine OpenCon() fails, you'll drop into the finally without a valid regKey.
remedy: perform a null-test in the finally.
in future: add debug/log statements, or single-step through your code. You can easily
solve this kind of problems yourself.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I appreciate your help, I added the code to check for null values. Another question I am hoping maybe you can help with is, I added debug code to help me sort out any further issues and I noticed that the line that says:
rdr = cmd.ExecuteReader();
fails to execute, it drops immediatley into the catch block, do you have any ideas why that may be happening.
Thanks,
Travis
|
|
|
|
|
Hi,
when you catch an exception you should display and log the entire exception.ToString(),
not just parts of it. That probably will tell you some.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
CREATE PROCEDURE msp_AddPreferredProperty
i have this code. I want that if the property and username exists it wont be added for 2nd time, but if they dont exists i want them to be added in the table
thanks
@CustomerUsername varchar(50),
@PropertyId int
As
if EXISTS
( Select count(*) From tblPreferredProperty Where CustomerUsername = @CustomerUsername
And PropertyId = @PropertyId)
Begin
Insert into tblPreferredProperty(PropertyId, CustomerUsername)
values(@PropertyId, @CustomerUsername)
End
GO
|
|
|
|
|
Looks not far off to me. Try this:
create proc msp_AddPreferredProperty
(
@CustomerUsername varchar(50),
@PropertyId int
)
as
if not exists(select 1 from tblPreferredProperty where CustomerUsername = @CustomerUsername And PropertyId = @PropertyId)
insert into tblPreferredProperty(PropertyId, CustomerUsername) values (@PropertyId, @CustomerUsername)
go
Regards,
Rob Philpott.
|
|
|
|
|
BTW - this should really be in the SQL forum.
Regards,
Rob Philpott.
|
|
|
|
|
Hi!
I have the following classes:
<br />
[XmlRoot("shoppingList")]<br />
public class ShoppingList<br />
{<br />
private ArrayList listShopping;<br />
<br />
public ShoppingList()<br />
{<br />
listShopping = new ArrayList();<br />
}<br />
<br />
[XmlElement("item")]<br />
public Item[] Items<br />
{<br />
get<br />
{<br />
Item[] items = new Item[listShopping.Count];<br />
listShopping.CopyTo(items);<br />
return items;<br />
}<br />
set<br />
{<br />
if (value == null) return;<br />
Item[] items = (Item[])value;<br />
listShopping.Clear();<br />
foreach (Item item in items)<br />
listShopping.Add(item);<br />
}<br />
} <br />
<br />
public int AddItem(Item item)<br />
{<br />
return listShopping.Add(item);<br />
}<br />
}<br />
<br />
public class Item<br />
{<br />
[XmlElement("name")]<br />
public string name;<br />
[XmlElement("price")]<br />
public double price;<br />
<br />
public Item()<br />
{<br />
}<br />
<br />
public Item(string Name, string Price)<br />
{<br />
name = Name;<br />
price = Convert.ToDouble(Price);<br />
}<br />
}<br />
When I Serialize it I get the following XML back:
<br />
<?xml version="1.0" encoding="utf-8"?><shoppingList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><item><name>eggs</name><price>1.49</price></item><item><name>bread</name><price>0.99</price></item></shoppingList><br />
As you can see from the definition of the ShoppingList class, I have an attribute [XmlElement] defined before the Items property, with the "items" name. So in my XML every element is <item>. What can I do, so that when I serialize my ShoppingList object, each "item" element will have it's own name.
So, one element will be "Table1", next will be "Table2", and so on.
Any help greatly appreciated.
Thank you very much.
|
|
|
|
|
First of because the Items property returns an array you should use the XmlArray attribute class:
<br />
[XmlArray("Items")]<br />
public Item[] Items<br />
{<br />
}<br />
To specify a name for each item use the XmlAttribute class like this:
<br />
public class Item<br />
{<br />
[XmlAttribute("name")]<br />
public string name;<br />
....<br />
|
|
|
|
|
Hello.
I am using the Windows Encrypted File System to encrypt a file. I want to transfer this file across a network to another computer in its encrypted state so i am trying to use the OpenEncryptedFileRaw Read, Write and Close api methods. (http://msdn2.microsoft.com/en-us/library/aa365429(VS.85).aspx[^])
I've found an example that uses ReadEncryptedFileRaw to save a file in its encrypted state, which seems to work fine.(http://www.jensign.com/JavaScience/dotnet/rawefs/rawefs.cs[^])
However I am having trouble understanding the call back functions and the IntPtr's. I need to convert the raw efs file created from rawefs.cs back to the files original state using WriteEncrypedFileRaw, but all my attempts to write an import call back required by the WriteEncryptedFileRaw api have failed.
Has anyone used these api methods in C#, know of an example or just in general have any helpful advice as to how to go about writing such a call back?
Thanks.
|
|
|
|
|
When I run setup of a Windows Service installation screen. How can I catch the cancel button event for the “Set Service Login” screen?
Thanks in advance,
Ken
|
|
|
|
|
Hi,
I am trying to create a log file with the following stmt(in C#):
private LogWriter DataLog = new LogWriter(new System.IO.StreamWriter( @"C:\"+DateTime.Now.ToLocalTime()+".txt", false ));
I get the exception - "The given path's format is not supported."
when i try to access the DLL (with this stmt) in another console applic.
Can anyone suggest me what the problem could be?
Thanks,
Priya.
|
|
|
|
|
A datetime string has characters which are illegal when used in file names. For example "/" is not allowed.
You could either try using DateTime.Now.Ticks which would give you a value which contains only numeric characters. Or you could try something like this:
<br />
DateTime time = DateTime.Now;<br />
String filename = time.Year.ToString("0000") + time.Month.ToString("00") + time.Day.ToString("00") + time.Hour("00") + time.Minute("00") + time.Second("00");<br />
<br />
private LogWriter DataLog = new LogWriter(new System.IO.StreamWriter( @"C:\"+filename+".txt", false ));<br />
A third option would be to use the String.Replace method to remove spaces ' ', colons ':' and forward slashes '/' from the datetime string.
Mark's blog: developMENTALmadness.blogspot.com
Funniest variable name:
lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher
|
|
|
|
|
Mark J. Miller wrote: String filename = time.Year.ToString("0000") + time.Month.ToString("00") + time.Day.ToString("00") + time.Hour("00") + time.Minute("00") + time.Second("00");
do you go round the block to reach you're neighbour's house?
string fileName = time.ToString("yyyyMMddhhmmss");
|
|
|
|
|
J4amieC wrote: do you go round the block to reach you're neighbour's house?
You're right, my answer was a kludge, and I don't know why I forgot about the DateTime overload. I appreciate you pointing out the right way to do it, that's why I try and post answers on these forums - to try and improve my skills by solving problems and getting feedback. I need it because I've never worked in a place where peer reviews are done and am 100% self-taught.
But there's no need to be so condesending.
Mark's blog: developMENTALmadness.blogspot.com
Funniest variable name:
lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher
|
|
|
|
|
Mark J. Miller wrote: I need it because I've never worked in a place where peer reviews are done and am 100% self-taught.
Sounds pretty similar to me.
Mark J. Miller wrote: But there's no need to be so condesending.
Sorry, you took it like that. It was supposed to be sarcastic humour! FAIL!
|
|
|
|
|
Well, maybe I need to work on my sense of humor. When I compare my answer with what it should have been I feel pretty silly. I'll just blame it on Monday.
Mark's blog: developMENTALmadness.blogspot.com
Funniest variable name:
lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher
|
|
|
|
|
Mark J. Miller wrote: But there's no need to be so condesending.
Maybe it'll drive the message home? I know it works for me.
|
|
|
|
|
Hi,
shivapriyak wrote: Can anyone suggest me what the problem could be?
it suffices to look at the string you are giving as a path to the StreamWriter constructor.
you could either show it (with Console.WriteLine, or MessageBox.Show; or use the debugger
to watch it).
You can create a valid string using the ToString() method of the DateTime class,
something like DateTime.Now.ToString("yyyyMMdd") would include the date info,
without any time info.
Read the doc for the details.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
modified on Monday, April 14, 2008 1:37 PM
|
|
|
|
|
Hi,
I am working with C#. In my project I need to take printout of a form in report fashon. This form comprises of pictures, texts, graphs and other controls. So I have decided to take the print out from the screenshot of the form. For doing that I have used bitblt() function first. But the printing quality with that is really very poor. So I have decided to use stretchblt() function for getting a better quality.
Now I need to take the device context of the available printer first for using the stretchblt() function successfully. But the usual getprinterdc() function (supported in MFC) is not working in C#. So plz tell me how can I solve this problem. I am using the following code:
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]<br />
public static extern int SetStretchBltMode(IntPtr hdcDest, int nStretchMode);<br />
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]<br />
public static extern int StretchBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int nWidthSrc, int nHeightSrc, System.Int32 dwRop);<br />
..<br />
..<br />
..<br />
private void CaptureScreen()<br />
{<br />
Graphics mygraphics = this.CreateGraphics();<br />
..<br />
..<br />
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = here I have to retrieve the available Printer DC as my destination DC
<br />
SetStretchBltMode(dc2, 4);
StretchBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height - 75, dc1, 0, 75, this.ClientRectangle.Width, this.ClientRectangle.Height, 13369376);
..<br />
..<br />
..<br />
memoryImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);<br />
}
Plz also let me know if u know any better solution of this problem
Thanks in advance
Faysal
|
|
|
|