|
You could bind the datagridview to a DataView. Use the filter property on the DataView to hide / show the rows you want based on your conditions.
|
|
|
|
|
Hi
I want to know if it is possible to import from the first sheet of an excel file regardless of the name of the sheet?
I currently import from excel but hard code the sheet name and the file i am importing from now, the sheet name changes every month.
Thank you for your assistance
Nothing lasts but nothing is lost
|
|
|
|
|
Are you using OleDb?
If so then use something like this:
DataTable schemaTable = _oleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string firstSheet = schemaTable.Rows[0]["TABLE_NAME"].ToString();
My failometer is detecting vast quantities of FAIL!
"Its SQL - hardly programming..." (Caslen)
|
|
|
|
|
thanx that did the trick
Nothing lasts but nothing is lost
|
|
|
|
|
I see you have a solution so you might want to ignore this
Did you know that in Excel each sheet has a 'Code Name' as well as a 'Sheet Name'?
The code name can only be set in the Excel designer (i.e. the VBA window) and stays the same regardless of how the user changes the sheet name.
I used this in VBA code to make sure the code (or macro in Excelese) still worked when users messed about with sheet names.
Not sure how to access it using OleDB but it should be possible.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Hi
Thanx
Is that the GUID property i saw in the datatable?
Nothing lasts but nothing is lost
|
|
|
|
|
No - it's a property that can be set in the VBA window in Excel. It's just a name which defaults to 'Sheet1' etc (IIRC). You can only change it in the VBA window - no way to do it in code or from the spreadsheet. I found it extremely useful when doing VBA macros in Excel (but that was 5 years ago!)
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Hi all,
I have a problem with custom classes in web services. The scenario is as follows:
I have created custom classes for use with my environment. We will take as an example, class student and class teacher which are in the package MyPackage.Data. These are being serialised at location A, passed to the web service and then sent to location B. While at the web service, the object is deserialised in order to extract info about the object to keep a log of what is going through. When the object is deserialised in the web service and then reserialised to send to location B (this also happens when creating a new object at the web service and sending it out to a location), the type of the object turns into MyWebservice.Student instead of MyPackage.Data.Student.
I get exceptions like: "Cannot cast object of type MyWebservice.Student to MyPackage.Data.Student".
I am guessing this is being taken from the reference file which is autogenerated on instantiation of a web service. An example of this auto generated code is shown below for the Student class
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3053")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="MyPackage.Date")]
public partial class Student{
private string Name;
public string Name{
get {
return this.name;
}
set {
this.name= value;
}
}
}
Now for this I used a work around, writing an alternate constructor for each class which takes an object of type MyWebservice.Student for example and creates a new instance of of MyPackage.Data.Student and sets all attributes to the attributes of the MyWebservice.Student object. An example is shown below:
public Starter(MyWebservice.Student webStudent)
{
this.name = webStudent.Name;
}
This works but is neither pleasant nor viable as a final solution. Can anybody please shed any light on how not to use the generated code of the web service or remove it completely or why this is happening.
Thanks a lot for your time and cooperation, any help is highly appreciated
|
|
|
|
|
Hello,
I have WSE 3.0 installed on my machine. I am having issues adding a Web Reference as a WSE 3.0 enabled web service. Does anyone have any good tutorials to accomplish this? All of my google results are for creating your own from scratch. I simply want to point to a wsdl file and have it create the resulting object under WSE 3.0 Thanks!!
|
|
|
|
|
i have a calendar that i click on one day, then when i have that day marked i write something in a textbox, after that i klick on my button and it should be saved, now my issue is:
i DO NOT want to create a new xmlfile dynamicly, i already have one xmlfile that i've added to the project, and i want it to store the day on the calendar that i clicked on, and the text that i wrote in my textbox, it should resoult like this:
<Plan 1><br />
< tuesday 5th may 2009 > today im going to write in codeproject forum < tuesday 5th may 2009><br />
<plan 1><br />
<br />
<plan 2 ><br />
< some day that i chose > blabla < some day that i chose><br />
<plan 2><br />
i hope you get the point?
so for each day i add a text on, i want it to generate a number ( i have already solve that problem )
int ID = 0;
while (readcalendar.Read())
{
if (readcalendar.Name.StartsWith("Plan"))
{
ID++;
}
}
and i also want to be able to Load the xmlfile each time i start my project, so that my "planings" is saved on the days i planed them. (ex. if i click on a day that i already have a plan on i want to be able to edit the text that already exist )
i have checked google for answers but i did not find anything and im a bit new to xml, so even if i've maybe stumbled on a correct answer i may have not looked into it.
if you dont understand my question, feel free to ask
|
|
|
|
|
Your XML does not look valid..
Also, what exactly is the question?
|
|
|
|
|
my question is: i have a calendar wich i want to store "text" on each day in,
example: i click on 5th may and write "i have to book a flight today " in a textbox, then i press a save-button (to store that information in a xmlfile), and if i open that xmlfile i want it to look like this:
-Plan1
--5th may 2009
i have to book a flight today
--/5th may 2009
-/plan1
(i already have a function that makes the plan1 increase in number so the next one look like this <plan2> then <plan3> and so on, so you dont have to write down that function)
and i also want the xml file that i just saved, to load each time i start my program, so that when i click on 5th may, i want the textbox to say "i have to book a flight today".
summary:
remember this is a almanac so i want the c# code to store information on the day i clicked on with the text i wrote in my textbox
|
|
|
|
|
You still don't really have a question though..
It's clear that there's something you want, well, ok, you're making it of course, and you're posting here so you're experiencing difficulties, but it's not yet clear what they are
Presumably you want to output:
<Plan1 date="5th may 2009">i have to book a flight today</Plan1>
(tags should start with a letter and can not contain spaces)
Ok, easy. You could even use an XmlWriter to make it easier (well, easier?)
Automatically loading the file: no problems here, if you want to be able to change the filename you could make it an application-setting.
Clicking the 5th of may: I guess you'd use a date picker control for that?
Making the textbox show something: just change the textbox.Text?
So I just tried to help with everything, since you didn't really specify your problem.
|
|
|
|
|
Miroslav88 wrote: i already have one xmlfile that i've added to the project, and i want it to store the day
Miroslav88 wrote: i also want to be able to Load the xmlfile each time i start my project
Miroslav88 wrote: im a bit new to xml
Go to www.w3schools.com and go through all the XML tutorials. Pay attention when you get to the ones about using an XML DOM.
|
|
|
|
|
Hi,
I'm just starting to convert my vb6.0 program to c#.net and I have a function in vb6.0 below:
Anyone can help me to translate this in c#.net.
Public Function GetData(SQL As String) As Variant
Dim RST As ADODB.Recordset
Set RST = Conn.Execute(SQL)
With RST
If .State = adStateOpen Then
If Not .EOF Then
If Not IsNull(RST(0)) Then
Select Case RST(0).Type
Case adVarChar
GetData = Trim(RST(0))
Case Else
GetData = RST(0)
End Select
Else
Select Case RST(0).Type
Case adBoolean
GetData = False
Case adVarChar
GetData = ""
Case adDouble, adInteger
GetData = 0
Case adDate, adDBTimeStamp
GetData = InvalidDate
End Select
End If
Else
Select Case RST(0).Type
Case adBoolean
GetData = False
Case adVarChar
GetData = ""
Case adDouble, adInteger
GetData = 0
Case adDate, adDBTimeStamp
GetData = InvalidDate
End Select
End If
Else
Select Case RST(0).Type
Case adBoolean
GetData = False
Case adVarChar
GetData = ""
Case adDouble, adInteger
GetData = 0
Case adDate, adDBTimeStamp
GetData = InvalidDate
End Select
End If
End With
Set RST = Nothing
End Function
THANKS AND REGARDS
klaydze
if(you type your code here) {
Messagebox.Show("You help me a lot!");
}
else {
You help me = null;
}
|
|
|
|
|
|
Le Centriste wrote: Do you even know C#?
Yeah but not that good as in VB6.
if(you type your code here) {
Messagebox.Show("You help me a lot!");
}
else {
You help me = null;
}
|
|
|
|
|
Hi,
I'm just starting to convert my vb6.0 program to c#.net and I have a function in vb6.0 below:
Anyone can help me to translate this in c#.net.
Public Function GetData(SQL As String) As Variant
Dim RST As ADODB.Recordset
Set RST = Conn.Execute(SQL)
With RST
If .State = adStateOpen Then
If Not .EOF Then
If Not IsNull(RST(0)) Then
Select Case RST(0).Type
Case adVarChar
GetData = Trim(RST(0))
Case Else
GetData = RST(0)
End Select
Else
Select Case RST(0).Type
Case adBoolean
GetData = False
Case adVarChar
GetData = ""
Case adDouble, adInteger
GetData = 0
Case adDate, adDBTimeStamp
GetData = InvalidDate
End Select
End If
Else
Select Case RST(0).Type
Case adBoolean
GetData = False
Case adVarChar
GetData = ""
Case adDouble, adInteger
GetData = 0
Case adDate, adDBTimeStamp
GetData = InvalidDate
End Select
End If
Else
Select Case RST(0).Type
Case adBoolean
GetData = False
Case adVarChar
GetData = ""
Case adDouble, adInteger
GetData = 0
Case adDate, adDBTimeStamp
GetData = InvalidDate
End Select
End If
End With
Set RST = Nothing
End Function
THANKS AND REGARDS
klaydze
if(you type your code here) {
Messagebox.Show("You help me a lot!");
}
else {
You help me = null;
}
|
|
|
|
|
A variant in VB is something that can be any type. The closest equivalent in C# is an 'Object'. In C# all data types derive from Object, so you can return the value as an Object and then cast it to what ever type it actually is.
(By the way, this is rather bad programming style, you should really be using parametrized queries, or stored procedures. This code is very vulnerable to SQL injection attacks. Also, by just chucking objects around everywhere you lose a lot of the benefits of strongly typed languages like C# such as compile time type checking. Also you seem to be repeating your select/case block about 3 times with the same code in it each time, it seems like you might have some redundant code here)
Simon
|
|
|
|
|
Simon Stevens wrote: A variant in VB is something that can be any type. The closest equivalent in C# is an 'Object'. In C# all data types derive from Object, so you can return the value as an Object and then cast it to what ever type it actually is.
(By the way, this is rather bad programming style, you should really be using parametrized queries, or stored procedures. This code is very vulnerable to SQL injection attacks. Also, by just chucking objects around everywhere you lose a lot of the benefits of strongly typed languages like C# such as compile time type checking. Also you seem to be repeating your select/case block about 3 times with the same code in it each time, it seems like you might have some redundant code here)
Hi Simon,
First, thanks for your reply.
I already converted my code to C#.Net. Yeah I used object as equivalent of variant.
My code something like this.
public object GetData(string SQL)
{
DataTable DT = new DataTable();
DT = dbConn.ExecuteQuery(SQL); dbConn is my connection class
if (!String.IsNullOrEmpty(DT.Rows[0][0].ToString()))
{
switch (DT.Column[0].DataType.ToString())
{
case "System.ToInt32":
return;
case "System.String":
return;
}
}
}
Any piece of advise?
The code just get the 1 field value in the query. Just thinking this is the best way, the shortest way of getting the 1 value in a query.
Example:
string sName;
sName = GetData("Select txtFirstname From tblUsers Where intID = 1").ToString();
Regards,
klaydze
if(you type your code here) {
Messagebox.Show("You help me a lot!");
}
else {
You help me = null;
}
|
|
|
|
|
klaydze wrote: Any piece of advise?
It's unmaintainable and insecure code.
What if some nasty user comes along and calls
GetData("DROP DATABASE [yourdatabase]") And this is just a minor problem. If you expose the ability to execute arbitrary SQL against your database you can wave goodbye to any serious form of security or data integrity.
If your just writing a prototype or for fun, don't worry about it too much. Just be aware the code is dangerous and should never grow into production code.
If this is production code I would advise a serious rethink about your architecture. You should create a separate data access layer. The DA layer should have strongly typed accessor methods, and they should being using pre written parametrized queries or stored procedures (Google those terms, it's pretty easy to use either with ADO.net)
E.g. you would have a UserDA class with a public User GetUser(String userName) method. The method would run the parametrized query and create a User object to be returned to the caller. This prevents a lot of issues. 1) You are only passing in the user name, not the whole SQL string so the caller can't execute whatever SQL they want. 2) You are returning a strongly typed User object so the caller gets exactly what they are expecting. 3) The use of stored procs or parametrized queries prevents SQL injection attacks. 4) You can build in security if you want and only allow certain users to retrieve and modify certain data.
Simon
|
|
|
|
|
Simon Stevens wrote: It's unmaintainable and insecure code.
What if some nasty user comes along and calls
GetData("DROP DATABASE [yourdatabase]")
And this is just a minor problem. If you expose the ability to execute arbitrary SQL against your database you can wave goodbye to any serious form of security or data integrity.
If your just writing a prototype or for fun, don't worry about it too much. Just be aware the code is dangerous and should never grow into production code.
If this is production code I would advise a serious rethink about your architecture. You should create a separate data access layer. The DA layer should have strongly typed accessor methods, and they should being using pre written parametrized queries or stored procedures (Google those terms, it's pretty easy to use either with ADO.net)
E.g. you would have a UserDA class with a public User GetUser(String userName) method. The method would run the parametrized query and create a User object to be returned to the caller. This prevents a lot of issues. 1) You are only passing in the user name, not the whole SQL string so the caller can't execute whatever SQL they want. 2) You are returning a strongly typed User object so the caller gets exactly what they are expecting. 3) The use of stored procs or parametrized queries prevents SQL injection attacks. 4) You can build in security if you want and only allow certain users to retrieve and modify certain data.
Hi Simon,
Thanks for the advise.
In short don't passed a sql statement in a parameter method? How about if I want to create a method that is accessible in all my form the same with my previous code.
public object GetData(string SQL) . How do I secured it?
In your example public User GetUser(string sUser), your just passing a single field where "sUser" is my filter in my sql statement inside the User Method.
SELECT * FROM tblUsers WHERE txtUser = sUser
Thanks and Regards
klaydze
if(you type your code here) {
Messagebox.Show("You help me a lot!");
}
else {
You help me = null;
}
|
|
|
|
|
But you should be using parametrized queries not just build up the sql statement on your own out of strings.
Simon
|
|
|
|
|
Simon Stevens wrote: But you should be using parametrized queries not just build up the sql statement on your own out of strings.
Hi Simon,
Sorry but I don't get it. "-(
Regards
klaydze
if(you type your code here) {
Messagebox.Show("You help me a lot!");
}
else {
You help me = null;
}
|
|
|
|
|
Use a SqlCommand and insert parameters into the command.
like this:
public static String GetUser(String userId)
{
SqlConnection connection = new SqlConnection();
String sqlQuery = "SELECT userName FROM Users WHERE UserId = @UserIdParameter";
SqlCommand command = new SqlCommand(sqlQuery, connection);
command.Parameters.Add(new SqlParameter("@UserIdParameter", userId));
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
return reader.GetString(0);
}
else
{
return String.Empty;
}
}
} This means that your caller can't inject sql in because they only have control over the parameter and because it's a parameter, when it the query is executed, the parameter will be validated and surrounded with quotes and any command characters will be removed to prevent injection attacks.
Read more here:
SQL Injection Attacks and Some Tips on How to Prevent Them[^]
Simon
|
|
|
|