|
you r right but perhaps a sample would be good enough to make newbie happy. However, I found the solution.....Had to declared a method in the baese class and call that method from the derived class and passed the information to the base class
|
|
|
|
|
I am reading the contents of excel file and storing contents in database.
I am using two datatables org_dt and grid_dt.
i am having a problem while adding rows to grid_dt datatable, also having problem while inserting date into this table.
here is the source code:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using Excel;
namespace WinApp_STRINGSEPARATE
{
public partial class Frm_admin_upload_optn : Form
{
string connectionString;// @" Dsn=Excel Files;dbq=C:\Program Files\Common Files\ODBC\Data
Sources\book4.xls;defaultdir=c:\;driverid=790;maxbuffersize=2048;pagetimeout=5";
OdbcConnection conn = new OdbcConnection();
OdbcCommand cmd = new OdbcCommand();
OdbcDataReader dr;
System.Data.DataTable org_dt = new System.Data.DataTable();
System.Data.DataTable grid_dt = new System.Data.DataTable();
string project_no = "", summary = "";
string[] divpara = new string[3];
string[] yes = new string[4];
public Frm_admin_upload_optn()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
}
private void Frm_admin_upload_optn_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
try
{
connectionString = @"Dsn=Excel Files;dbq=" + textBox1.Text.Trim()
+";driverid=790;maxbuffersize=2048;pagetimeout=5";
conn.ConnectionString = connectionString;
cmd.CommandText = "SELECT * FROM [sheet1$]";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = conn;
conn.Open();
dr = cmd.ExecuteReader();
org_dt = new System.Data.DataTable();
if (dr.HasRows)
{
org_dt.Load(dr);
grid_dt.Columns.Add("PROJECT NO");
grid_dt.Columns["PROJECT NO"].DataType = typeof(string);
grid_dt.Columns.Add("TICKET NO");
grid_dt.Columns["TICKET NO"].DataType = typeof(string);
grid_dt.Columns.Add("REPORTED DATE");
grid_dt.Columns["REPORTED DATE"].DataType = typeof(DateTime);
grid_dt.Columns.Add("OLD COMPUTER NAME");
grid_dt.Columns["OLD COMPUTER NAME"].DataType = typeof(string);
grid_dt.Columns.Add("NEW COMPUTER NAME");
grid_dt.Columns["NEW COMPUTER NAME"].DataType = typeof(string);
DataRow grid_dt_row = grid_dt.NewRow();
for (int j = 0;j<4;j++ )
grid_dt_row[j] = "";
int count_orgdt = org_dt.Rows.Count;
int i = 0;
foreach (DataRow drow in org_dt.Rows)
{
summary = drow["summary"].ToString();
if (summary != "")
{
if (summary.Contains("Project #-"))
project_no = "Project #-";
else project_no = "Project Number #-";
divpara[0] = project_no;
divpara[1] = "New PC Name :";
divpara[2] = "Old PC Name :";
yes = summary.Split(divpara, System.StringSplitOptions.None);
//grid_dt.Rows.Add(grid_dt_row);
grid_dt.ImportRow(grid_dt_row); //ERROR MSG
grid_dt.Rows[i]["project no"] = yes[1];
grid_dt.Rows[i]["ticket no"] = drow[0];
grid_dt.Rows[i]["reported date"] =drow[3] ; // ERROR RELATED TO DATE FIELD
grid_dt.Rows[i]["old computer name"] = yes[2];
grid_dt.Rows[i]["new computer name"] = yes[3];
i++;
}
}
dataGridView1.DataSource = grid_dt;
}
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
finally
{
conn.Close();
}
}
}
}
|
|
|
|
|
The error: "This row belongs to another table" is legitimate because the row contains a reference to the table that initiated it. (row.Table) If you are wanting to copy the row's data into another table with the same schema, try this syntax:
secondTable.Rows.Add(firstTableRow.ItemArray());
As to the date error, it would help if you could post the error text.
Cheers!
Richard Blythe
I've used up all my sick days, so today I'm calling in dead.
|
|
|
|
|
I created a combobox that binds to a datasource. If the combobox only contains one item, I programatically select the one item and want to trigger SelectionChangeCommitted event just as though the user had selected it. How can I call this event manually?
|
|
|
|
|
The OnSelectionChangeCommitted that raises the event is protected virtual so it is only accesible in derived classes. Therefore, I think the only way to call that method to raise the event is by your own class the derives from ComboBox:
public class MyComboBox : ComboBox
{
public void RaiseSelectionChangeCommitted()
{
base.OnSelectionChangeCommitted(EventArgs.Empty);
}
}
You can now use the RaiseSelectionChangeCommitted method to call the base method and raise the event. This is probably not advisable and there is probably a better way but it's all I can think of - and it works!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
DaveyM69 wrote: This is probably not advisable and there is probably a better way but it's all I can think of - and it works!
If it works, do it![^]
led mike
|
|
|
|
|
LMFAO!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Thanks! That's exactly what I was trying to do.
|
|
|
|
|
Is List<object> fundametally the same as ArrayList or should I prefer one over the other?
|
|
|
|
|
|
Man, you're about as popular with the 1-voters as John the Outlaw Programmer is. Are you running for his office??
|
|
|
|
|
Dave Kreskowiak wrote: Are you running for his office??
No, but perhaps I should care what some loser that doesn't even know what generics is and can't be bothered to read documentation to learn about it, thinks about my post? Nah, not so much.
led mike
|
|
|
|
|
The first, is a generic list specifically of type object. the second is a non generic list, written to hold object types.
If you use "object" in the generic one, then yes they are pretty similar. but why are you storing a list of objects? You should create you list with the specific type you want to store
i.e. List<String>
Doing this is wayyyy better than using an ArrayList to hold strings because the generic form enforces type safety, you don't have to cast, and you don't have to write a specific class. (And in the case of value types, prevents boxing)
You can do some reading on generics here:
Intro to C# generics[^]
Generics - C# programming guide[^]
Simon
|
|
|
|
|
I cannot tell the type of the data being collected at compile time. It could change based on the situation.
Brent
|
|
|
|
|
dbrenth wrote: I cannot tell the type of the data being collected at compile time.
Then use an ArrayList!
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
if you're working with pure objects only then I don't think there's a great deal of difference. If not, the generic list is obviuosly far more flexible and much quicker as you can use any type without boxing/unboxing.
As there is no advantage to the ArrayList I never use them. This page[^] (is probably quite old) has a comparison of the two.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
For the most part the way you interact with a List<T> and an ArrayList are the same. They provide very similar functionality to the calling code.
Specifically, List<Object> is defining a generic list of type Object so from a data type point of view it is the same as ArrayList since ArrayList can only contain objects.
In general, I think it is preferable to use the generics, particularly if you are working with strong data types (things other than objects) as you are able to get better performance from the generics because you no longer have most (if not all) of the boxing/unboxing operations that need to be performed. That being said, by creating a List<Object> , you probably aren't really gaining a whole lot over an ArrayList .
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
|
|
|
|
|
List<object> and ArrayList are very similar, but there are some implementation differences:
The List<object> methods are not virtual and the List enumerator is a value type, whereas ArrayList requires virtual method calls and needs a heap allocation for the enumerator. So List<object> is a tiny bit faster than ArrayList .
I'd always use List<T> , even if you don't avoid any boxing and don't get any type safety in the List<object> case.
|
|
|
|
|
If you're not able to appreciate the difference, use the ArrayList . The other way passes through led mike's suggestion.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
The generic List class has practically replaced the ArrayList class. Don't use the ArrayList class any more.
For the rare case where you actually need a list of object references, use List<object> instead of ArrayList. That way it's obvious that you needed exactly that kind of list, and that you are not just using ArrayList because you don't know any better.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I am trying to create a table in an Access database
I have verified all of my SQL commands work in SQL Server so I'm figuring that i have a formatting for C# issue i am over looking or doing something wrong via the OLDDB driver
here is my code
String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Test.mdb";
OleDbConnection con = new OleDbConnection(connect);
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "Create Table GroupPolicy(GUID char(50) NOT NULL Unique, GPONAME char(50), Domain Char(50), Created datetime, Modified datetime, Status char(10))";
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
con.Close();
the exception is telling me there is a syntax error
Thanks
|
|
|
|
|
Planker wrote: I am trying to create a table in an Access database
I have verified all of my SQL commands work in SQL Server
Access SQL is NOT EQUAL to SQL Server.
Therefore your test is invalid. Try testing your SQL in the Database you are targeting. That is always the best approach.
Good luck
led mike
|
|
|
|
|
Thanks, I am trying to figure out what the correct commands are, I see examples all over the internet using the SQL create table command on a Access using OLEDB, but everything i try does not work.
modified on Wednesday, October 8, 2008 2:02 PM
|
|
|
|
|
Planker wrote: Thanks, I am trying to figure out what the correct commands are
Sure, but wouldn't something like the documentation[^] be a good place to start?
led mike
|
|
|
|
|
Thanks for pointing me in the right direction, I am making some progress now.
|
|
|
|