|
|
Hello
I am filling a dataset from a single SQL table, containing some fields of datetime type. I then proceed reading each row in the dataset and attempting to pass the values of each record in a class, populating its fields. When I try to pass the datetime field into a class field of System.DateTime type, I try to also convert it to ToShortTime() format. This fails to compile, telling me that the datarow element is of type "object" and does not contain a definition for ToShortTime(). When I break execution just before the exception (after changing my syntax to ToString() and the type of the class field to string which compiles OK), I use the Immediate window to view the type of my datarow element and is shown to be ValueType: {System.DateTime}.
I am confused....any help please?
Regards
Chris
|
|
|
|
|
Although the field might contain a DateTime the compiler doesn't know that this column is filled with DateTime instances. And even if he knew there could still be some rows where this field is empty. In that case the value would be DbNull.
This means you just have to cast the object value you get to tell the compiler explicitely what he not able to determine. You could do something like this:
if (dataRow[fieldIndex] == DbNull.Value) {
//field is empty - do wahtever you think has to be done
} else {
DateTime date = (DateTime)dataRow[fieldIndex];
string timeAsShortString = date.ToShortTime();
}
|
|
|
|
|
This worked. Thank you very much.
It appears that the compiler does not realize (or auto-cast) dataset column element types read directly from the table.
Once again thanks a lot.
Chris
|
|
|
|
|
I have to generate printable reports from an Access database (Crystal Reports is not an option). Using automation, I can show an Access report, but that leaves my Access database open -- not sure I should trust my user(s) that much.
I'm not stuck to the idea of using an Access report, I just want to be able to easily create 6-10 reports that can be printed (displayed before printing would be nice, but not required).
Any pointers would be helpful.
Thanks,
Glenn
--
glenn_lanier@netzero.net
|
|
|
|
|
Hope this helps. You may need to change it a bit to meet your needs such as the Access version, but here are 2 routines that 1 prints directly and the other does a preview.
The reports are created in access then these call upon those report names.
Also it's in vb.net !sorry! but I also read/write C# and thought that this might be helpful to you.
Private Sub Print_Report(ByVal _report As String)
'Prints the "Routing Report" in ticketaccess.mdb.
' Enable an error handler for this procedure:
On Error GoTo ErrorHandler
'Dim oAccess As Access.Application
'path to ticketaccess.mdb file
Dim cfg As ConfigInfo = GetConfig
Dim sDBPath As String = cfg.databasepath
Dim sReport As String = _report 'name of report to print"
' Start a new instance of Access for automation:
'oAccess = New Access.Application()
Dim oAccess = CreateObject("Access.application.8")
' Determine the path to ticketaccess.mdb:
'sDBPath = oAccess.SysCmd(Action:=Access.AcSysCmdAction.acSysCmdAccessDir)
'sDBPath = sDBPath & "ticketaccess.mdb"
' Open ticketaccess.mdb in shared mode:
oAccess.OpenCurrentDatabase(filepath:=sDBPath, Exclusive:=False)
' Select the report name in the database window and give focus
' to the database window:
oAccess.DoCmd.SelectObject(ObjectType:=Access.AcObjectType.acReport, _
ObjectName:=sReport, InDatabaseWindow:=True)
' Print the report:
oAccess.DoCmd.OpenReport(ReportName:=sReport, _
View:=Access.AcView.acViewNormal)
Cleanup:
' Quit Access and release object:
On Error Resume Next
oAccess.Quit(Option:=Access.AcQuitOption.acQuitSaveNone)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
oAccess = Nothing
Exit Sub
ErrorHandler:
MsgBox(Err.Number & ": " & Err.Description, _
MsgBoxStyle.MsgBoxSetForeground, "Error Handler")
' Try to quit Access due to an unexpected error:
Resume Cleanup
End Sub 'Uses report in access db
Private Sub Preview_Report(ByVal _report As String)
'Previews the "Summary of Sales by Year" report in ticketaccess.mdb.
' Enable an error handler for this procedure:
On Error GoTo ErrorHandler
'Dim oAccess As Access.Application
Dim oForm As Access.Form
'path to ticketaccess.mdb file
Dim cfg As ConfigInfo = GetConfig
Dim sDBPath As String = cfg.databasepath
Dim sReport As String = _report 'name of report to preview
' Start a new instance of Access for automation:
'oAccess = New Access.ApplicationClass()
Dim oAccess = CreateObject("Access.Application.8")
' Make sure Access is visible:
If Not oAccess.Visible Then oAccess.Visible = True
' Determine the path to ticketaccess.mdb:
'sDBPath = oAccess.SysCmd(Action:=Access.AcSysCmdAction.acSysCmdAccessDir)
'sDBPath = sDBPath & "ticketAccess.mdb"
' Open ticketaccess.mdb in shared mode:
oAccess.OpenCurrentDatabase(filepath:=sDBPath, Exclusive:=False)
' Close any forms that ticketaccess may have opened:
For Each oForm In oAccess.Forms
oAccess.DoCmd.Close(ObjectType:=Access.AcObjectType.acForm, _
ObjectName:=oForm.Name, _
Save:=Access.AcCloseSave.acSaveNo)
Next
If Not oForm Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(oForm)
End If
oForm = Nothing
' Select the report name in the database window and give focus
' to the database window:
oAccess.DoCmd.SelectObject(ObjectType:=Access.AcObjectType.acReport, _
ObjectName:=sReport, InDatabaseWindow:=True)
' Maximize the Access window:
oAccess.RunCommand(Command:=Access.AcCommand.acCmdAppMaximize)
'oAccess.RunCommand(Command:=Access.AcCommand.acCmdAppMinimize)
' Preview the report:
oAccess.DoCmd.OpenReport(ReportName:=sReport, _
View:=Access.AcView.acViewPreview)
' Maximize the report window:
oAccess.DoCmd.Maximize()
' Hide Access menu bar:
oAccess.CommandBars("Menu Bar").Enabled = False
' Hide Report's Print Preview menu bar:
oAccess.CommandBars("Print Preview").Enabled = False
' Hide Report's right-click popup menu:
'oAccess.CommandBars("Print Preview Popup").Enabled = False
' Release Application object and allow Access to be closed by user:
If Not oAccess.UserControl Then oAccess.UserControl = True
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
oAccess = Nothing
Exit Sub
ErrorCleanup:
' Try to quit Access due to an unexpected error:
On Error Resume Next
System.Runtime.InteropServices.Marshal.ReleaseComObject(oForm)
oForm = Nothing
oAccess.Quit(Option:=Access.AcQuitOption.acQuitSaveNone)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
oAccess = Nothing
Exit Sub
ErrorHandler:
MsgBox(Err.Number & ": " & Err.Description & "- -" & sDBPath, _
MsgBoxStyle.MsgBoxSetForeground, "Error Handler")
Resume ErrorCleanup
End Sub ' Uses report in access db
|
|
|
|
|
Thanks; that is a VB solution using automation. I have a C# solution (of sorts) that does the same thing. What I'm really looking for is a way to embed the report in my C# application.
|
|
|
|
|
Embed a report? What type of report? Are you looking to take information and save to a file then print it? I guess I don't fully understand your situtation.......
|
|
|
|
|
The ideal solution would be to present the results of an Access report in a window I control -- so it looks like part of my application, not by using automation and having Access open.
I keep referring to Access because the data is in an Access DB, and the client has existing reports (data is invalid/incorrect, but that's a different story) they are satisfied with.
If there is a non-Crystal Reports option (read, free and easy) that I can use, I don't mind recreating the reports. I thought about generating them as HTML and displaying in a WebBrowser control, but the support for page headers/footers just isn't there yet, AFAIK.
I can save the data to a file and manipulate as needed, I suppose.
Hope this helps explain what I'm looking for.
--G
|
|
|
|
|
Just a suggestion but what about xml. You could post the data to an xml doc, attach a style sheet and produce an html report that could be rendered in a browser object.....
|
|
|
|
|
KaptinKrunch wrote:
produce an html report that could be rendered in a browser object
Thanks for the suggestion, but with HTML there is very little support (especially by IE, the browser object I'd have to use) for printing control, meaning page headers/footers.
I'm probably going to go with just automating Access and try to train the few users not to monkey around with anything in the database.
|
|
|
|
|
Ok I've been lurking around your site for a while now and the articles/tutorials have been a great help, but i've seem to run into.. something..
I'm trying to learn how to program for DirectX in C#. I only plan to use DirectDraw at the moment, and I think I've started to get a decent understanding of how it all works.
Now for the problem....
The problem started when I was getting this error:
- Object reference not set to an instance of an object
Now that I understand what the compiler is saying is wrong i was able to make that error go away.. though i don't fully understand why...lol.
Appearently all i needed was:
- SpriteSet[(int)Sprite.BlueBlob] = new classSprite();
but i thought it already did that when I did this:
- private classSprite[] SpriteSet = new classSprite[2];
So if anyone could explain that to me, it would be greatly appreciated.
The new problem is:
- Value does not fall within the expected range
when i try to do this:
TempSurface = new Surface(SpriteSet[(int)Sprite.BlueBlob].FilePath, description, display);
I have stripped this program many times, trying to find out what i needed to change but have been unable to find anything. plz help me!
I've only pasted here what i think you may need to solve my problem... if there is a better way you'd like me to post it or if you need more, just say.
The line the Error occurs is at the bottom of the code.
<br />
public class classSprite<br />
{<br />
public string FilePath;<br />
public int Height = new int();<br />
public int Width = new int();<br />
public Surface SpriteSurface;<br />
public SurfaceDescription desc = new SurfaceDescription();<br />
public int tFrames = new int();<br />
public Rectangle[] frames;<br />
public int[,] FrameRefrences = new int[4,8];<br />
public int[] AnimTotals = new int[4];<br />
}<br />
public class classUnit<br />
{<br />
public uint CurrentFrame;<br />
public uint CurAnim;<br />
public uint PrevAnim;<br />
public int AnimX;<br />
public int AnimY;<br />
public int UnitSpriteSet;<br />
public int Facing;<br />
}<br />
enum Sprite {BlueBlob, RedBlob};<br />
enum Dir {N, NE, E, SE, S, SW, W, NW};<br />
enum Anim {Walk, Attack, Death, Dead};<br />
<br />
private classSprite[] SpriteSet = new classSprite[2];<br />
private classUnit[] Units = new classUnit[1]; <br />
<br />
private System.ComponentModel.IContainer components;<br />
private System.Windows.Forms.Timer AnimationTimer;<br />
private System.Windows.Forms.Label label1;<br />
<br />
private Device display = new Device();<br />
private Surface front = null;<br />
private Surface back = null;<br />
<br />
private Surface TempSurface = null;<br />
private Clipper clip = null; <br />
<br />
<br />
private void InitDirectDraw()<br />
{<br />
SurfaceDescription description = new SurfaceDescription();<br />
display = new Device();<br />
#if DEBUG<br />
display.SetCooperativeLevel(this,CooperativeLevelFlags.FullscreenExclusive);<br />
#else<br />
display.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive);<br />
display.SetDisplayMode(640, 480, 16, 0, false);<br />
#endif<br />
description.SurfaceCaps.PrimarySurface = true;<br />
#if DEBUG<br />
front = new Surface(description, display);<br />
#else<br />
description.SurfaceCaps.Flip = true;<br />
description.SurfaceCaps.Complex = true;<br />
<br />
description.BackBufferCount = 1;<br />
<br />
front = new Surface(description, display);<br />
#endif<br />
description.Clear();<br />
#if DEBUG<br />
description.Width = front.SurfaceDescription.Width;<br />
description.Height = front.SurfaceDescription.Height;<br />
description.SurfaceCaps.OffScreenPlain = true;<br />
back = new Surface(description, display);<br />
#else<br />
SurfaceCaps caps = new SurfaceCaps();<br />
caps.BackBuffer = true;<br />
<br />
back = front.GetAttachedSurface(caps);<br />
#endif<br />
clip = new Clipper(display);<br />
clip.Window = this;<br />
front.Clipper = clip; <br />
<br />
description.Clear();<br />
SpriteSet[(int)Sprite.BlueBlob] = new classSprite();<br />
SpriteSet[(int)Sprite.BlueBlob].FilePath = Application.StartupPath + "\\BlueBlob_Sequence.bmp";<br />
SpriteSet[(int)Sprite.BlueBlob].Height = 40;<br />
SpriteSet[(int)Sprite.BlueBlob].Width = 40;<br />
TempSurface = new Surface(SpriteSet[(int)Sprite.BlueBlob].FilePath, description, display);
SpriteSet[(int)Sprite.BlueBlob].SpriteSurface = TempSurface;<br />
SpriteSet[(int)Sprite.BlueBlob].desc = description;<br />
SpriteSet[(int)Sprite.BlueBlob].tFrames = 152;<br />
SpriteSet[(int)Sprite.BlueBlob].frames = new Rectangle[SpriteSet[(int)Sprite.BlueBlob].tFrames];<br />
SpriteSet[(int)Sprite.BlueBlob].AnimTotals[(int)Anim.Walk] = 10;<br />
SpriteSet[(int)Sprite.BlueBlob].AnimTotals[(int)Anim.Attack] = 5;<br />
SpriteSet[(int)Sprite.BlueBlob].AnimTotals[(int)Anim.Death] = 4;<br />
SpriteSet[(int)Sprite.BlueBlob].AnimTotals[(int)Anim.Dead] = 1;<br />
}<br />
}
|
|
|
|
|
YawgmothIII wrote:
Now that I understand what the compiler is saying is wrong i was able to make that error go away.. though i don't fully understand why...lol.
Appearently all i needed was:
- SpriteSet[(int)Sprite.BlueBlob] = new classSprite();
but i thought it already did that when I did this:
- private classSprite[] SpriteSet = new classSprite[2];
So if anyone could explain that to me, it would be greatly appreciated.
The new problem is:
- Value does not fall within the expected range
when i try to do this:
TempSurface = new Surface(SpriteSet[(int)Sprite.BlueBlob].FilePath, description, display);
Hi - welcome to CP !!
private classSprite[] SpriteSet = new classSprite[2]; creates an array of two classSprites.
SpriteSet[(int)Sprite.BlueBlob] = new classSprite(); this assumes SpriteSet exists as an array already, and assigns the classSprite at the position Sprite.BlueBlob ( which must be 0 or 1 ) to be a new classSprite. This is obviously a class of your own design, is there some reason why you'd need to do both, that is, why you'd need to call the constructor for every item in the array ?
"Value does not fall within the expected range"
At a guess, this means that SpriteSet does not contain enough values for there to be one at Sprite.BlueBlob.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
This is obviously a class of your own design, is there some reason why you'd need to do both, that is, why you'd need to call the constructor for every item in the array ?
If I understand you correctly, then no i don't believe there is a reason, but that just happens to be the way i figured out how to make "Object reference not set to an instance of an object" go away. I didn't think i would have to do both either, but if i don't "Object reference not set to an instance of an object" pops up on everyone of the classes..uh..objects?.
And about the second problem, i have taken the (int)Sprite.BlueBlob out and replaced it with a ZERO like it should be, but it does the same thing. "Value does not fall within the expected range" when i try to create a surface.
...help..me...
|
|
|
|
|
Anonymous wrote:
And about the second problem, i have taken the (int)Sprite.BlueBlob out and replaced it with a ZERO like it should be, but it does the same thing. "Value does not fall within the expected range" when i try to create a surface.
Then it must be something wrong with the data being passed in to the method. Look at the parameters you're passing in to the constructor, and then read MSDN to see what the valid ranges of values are that you can pass in.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I FINALLY figured it out.
When i entered this:
SpriteSet[(int)Sprite.BlueBlob].SpriteSurface = new Surface(SpriteSet[(int)Sprite.BlueBlob].FilePath, description, display);<br />
SpriteSet[(int)Sprite.BlueBlob].FilePath was equal to Application.StartupPath + "\\Blob_Sequence.bmp"
Unfortuantely Blob_Sequence.bmp is not the name of my file.. its BlueBlob_Sequence.bmp.
I never would have guessed that, that would give me that kind of error.
thanks for your help!
ugh.. my brain hurts...but in a good way... i think?
|
|
|
|
|
Yeah, that's a totally dumb error to return. Why not 'file does not exist' ?
Anonymous wrote:
thanks for your help!
Dunno if I helped, but I was happy to try
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hey everybody!
I wondered if it is possible to use windows' auto-complete.
I don't mind if it'll be using web application or not.
we all know the option of "remember my password" in mail accounts and many other places, and the listbox that opens under textbox with old texts that the user entered.
is there a way to use that database in c# win application or c# web application?
if there is can someone help me with that (tutorial, or somekind of explanation).
Thanks Alot!!!
|
|
|
|
|
|
Thanks aLOT !!!
just one more question, do you know any other tutorial for web apps?
Thanks!
|
|
|
|
|
Good afternoon:
Have you ever used SMS
Systems Management Server?
If you have then you know you can connect to another PC and interact with the Desktop.
I would like to know how this is done and are there any good articles?
I have looked under Video Buffer, Video Stream but just cant find what I am looking for.
Thank you very much,
William O'Malley
|
|
|
|
|
|
Tried using ContextMenu and ToolBar controls, utilizing ownerdrawn = true to render a matrix of shape icons in-place-of the owner drawn MenuItem.
The problem is there is no way to correlate the mouse click coords to that of item selected in the ownerdrawn MenuItem.
Looking for some sample code in which someone has attempted to create an MS Word AutoShape "like" control.
Thanks
|
|
|
|
|
Hi all,
I am trying to connect to an ACCESS database and read data from it. I believe the connection opens and closes fine, but when I add the line to Read from the data (ExecuteReader) I get the following exception error:
[An unhandled exception of type 'Microsoft.Data.Odbc.OdbcException' occurred in microsoft.data.odbc.dll]
I also downloaded and installed the latest MDAC and the ODBC driver (unless there is other stuff to be installed I am not aware of!)
And it is not an SQL-command error because (and this is also interesting) when I copy the table to my C: drive, everything works fine and I get the results I am looking for, even though it is pointing to the I: drive (the local server where the data is stored)
////////////here is the code:
string connection= "Driver={Microsoft Access Driver (*.mdb)};"
+ "Dbq=I:\\ACCESS2000\\US_MASPS.mdb;" ;
OdbcConnection myConnection = new OdbcConnection(connection);
myConnection2.Open();
string select = "SELECT * from US_MASPS.TBLGMS"
+" WHERE SeqNo = 12";
OdbcCommand aCommand = new OdbcCommand(select, myConnection);
OdbcDataReader aReader = aCommand.ExecuteReader();
/////////////
Thanks in advance
|
|
|
|
|
I would recommend moving from a DBQ connection string to the following:
Standard security:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;User Id=admin;Password=;"
Workgroup (system database):
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;Jet OLEDB:System Database=system.mdw;"
With password:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;Jet OLEDB:Database Password=MyDbPassword;"
Also check out www.connectionstrings.com
|
|
|
|
|