Hello:
I am using the following code to get a record. In the DBDataAdapter , how do I get the specific field I want? It does not like the ReadRecord.
DBSQLSelect = "SELECT "
+ " [SEQNO]"
+ " ,[Status_ID]"
+ " ,[Class_ID]"
+ " ,[Group_ID]"
+ " ,[Type_ID]"
+ " ,[CreateBy]"
+ " ,[CreateDate]"
+ " ,[CreateTime]"
+ " ,[Note]"
+ " ,[Address_1]"
+ " ,[Address_2]"
+ " ,[Address_3]"
+ " ,[City]"
+ " ,[State_ID]"
+ " ,[Zip_ID"
+ " ,[Phone]"
+ " ,[Fax]"
+ " ,[WebSite]"
+ " FROM"
+ " [" + FileName + "]"
+ " WHERE"
+ " [Company_ID] = '" + textBoxCompany_ID.Text + "'"
;
//MessageBox.Show("DBSQLSelect=" + DBSQLSelect);
//
Program.SQLSelect(DBSQLSelect);
//
SqlDataAdapter DBDataAdapter = new SqlDataAdapter(Program.ProgramDBSelect, Program.ProgramDBConnection);
SqlDataReader ReadRecord;
textBoxName.Text = ReadRecord.GetString(ReadRecord.GetOrdinal("Name"));
Loading
theLizardPosted Mar 15, 2010, 10:12 PM
ExecuteReader: Connection property has not been initialized."
MyCommand.Connection = MyConnectio;
GustavoPosted Mar 16, 2010, 12:38 AM
If you would of told me where to put that line, it would of been a lot simpler for all of us.
Yes, I did not have the line: SQLCommand.Connection = SQLConnect;
Now I know... thanks.
GustavoPosted Mar 16, 2010, 12:00 AM
Ok, I took all the stuff I had in there and put it back to where I initially had it. I am not using any of my own classes. Just the system ones. Below is the code:
The error I am getting is: {"ExecuteReader: Connection property has not been initialized."}. The line is the one marked "<<
//---------------------------------------------------------
DBSQLSelect =
"SELECT "
+ " [SEQNO]"
+ " ,[Name]"
+ " ,[Address_1]"
+ " ,[Address_2]"
+ " FROM"
+ " [" + Program_ID + "]"
+ " WHERE"
+ " [Company_ID] = '" + textBoxCompany_ID.Text + "'"
;
DBConnection =
@"Server=" + DBServer + ";"
+ "Database=" + DBDatabase + ";"
+ "User ID=" + DBLogin_ID + ";"
+ "Password=" + DBPassword + ";";
SqlDataReader ReadRecord = null;
SqlConnection SQLConnect = new SqlConnection(DBConnection);
SQLConnect.Open();
SQLConnect.GetSchema();
SqlCommand SQLCommand = new SqlCommand(DBSQLSelect);
ReadRecord = SQLCommand.ExecuteReader(); //<<
textBoxName.Text = (
//---------------------------------------------------------
Sam HobbsPosted Mar 15, 2010, 10:24 PM
It has been a long time since I have heard GIGO but I assume you have.
GustavoPosted Mar 15, 2010, 10:08 PM
Ok, I will use what you said: ReadRecord.GetString("Name");
But it does not get there. The porgram stops at:
ReadRecord = MyCommand.ExecuteReader(); //<<< {"ExecuteReader: Connection property has not been initialized."}
GustavoPosted Mar 15, 2010, 10:01 PM
The error is still in the same place.
//---------------------------------------------------------
//using System;
//using System.Data;
//using System.Data.SqlClient;
string DBConnection =
@"Server=" + Program.ProgramDBServer + ";"
+ "Database=" + Program.ProgramDBDatabase + ";"
+ "User ID=" + Program.ProgramDBLogin_ID + ";"
+ "Password=" + Program.ProgramDBPassword + ";";
DBSQLSelect = "SELECT "
+ " [SEQNO]"
+ " ,[Name]"
+ " ,[Address_1]"
+ " ,[Address_2]"
+ " FROM"
+ " [" + Program_ID + "]"
+ " WHERE"
+ " [Company_ID] = '" + textBoxCompany_ID.Text + "'"
;
SqlDataReader ReadRecord = null;
SqlConnection MyConnection = new SqlConnection(DBConnection);
SqlCommand MyCommand = new SqlCommand(DBSQLSelect);
MyConnection.Open();
ReadRecord = MyCommand.ExecuteReader(); //<<< {"ExecuteReader: Connection property has not been initialized."}
textBoxName.Text = (string) ReadRecord["Name"];
//---------------------------------------------------------
GustavoPosted Mar 15, 2010, 9:36 PM
I am using:
using System.Data;
using System.Data.SqlClient;
Should I add another one for the data read?
theLizardPosted Mar 15, 2010, 9:33 PM
rather than think what could be and what could not be
ReadRecord.GetString(ReadRecord.GetOrdinal("Name"));
he is asking for the value using GetString but where a field name is expected as a string he is giving a GetOrdinal, in other words and hopefully it will be simple for people to understand
ReadRecord.GetString("Name"); is all that is needed
NOT ReadRecord.GetString(1); where 1 is the ordinal position if name was the first field in the select statement, which is the same as ReadRecord.GetString(ReadRecord.GetOrdinal("Name"));
ReadRecord.GetOrdinal("Name") this would return the ordinal position in the result set.
ReadRecord.GetString("Name"); this gets the value of the fields content.
Do you understand...
theLizardPosted Mar 15, 2010, 9:26 PM
My issue is wasting time on people who have no idea on the direction they are going, repeatedly asking questions in differing ways, accepting answers going in completely different direction to what the answer is, was, will be.
I have a issue in people such as you, wanting help, getting it, then ignoring it.
I have issues with people such as yourself who even when the answer is staring at you in the face you come back and say it's still not working.
If you said you were 11 or 81 I would come back and say lets help, but you are not these ages and your whole line of questions is beginning to look like you are taking the mickey out of those wishing to help or you are not capable of formulating any of your own ideas as to what may be the problem with simple code like textBoxName.Text = ReadRecord.GetString(ReadRecord.GetOrdinal("Name")); where you still cannot see what is wrong.
The fact that you asked how to create multiple tables with the same 10 or so fields indicates that you really have no idea about relational database system because if you did you would have just one table with these fields. (BTW, it is REALLY simple to get SQL Server to copy the create table statement to the clip board)
The fact that you accepted a post of mine as an answer to your question and obviously using anything other than what was suggested in the post shows that you really have no idea which way you are going.
You accept posts by others then ask another question which is really the same question as that already answered but in a different way.
Unlike you, I have been writing relational databases for a very long time and what you are doing seems nonsensical, but I am sure you have your reasons for doing things the way that you are, after all you are here asking questions about how to do things in the programming sense, but I have not once heard you ask about designing systems that a human would want to use.
Your conclusion is also interesting, where I come from we would have said go see your doctor (physician) you on the other hand talk in the plural sense which implies you may have multiple physician, now of course I could have multiple physicians if I had more than one issue but you have said I have an issue, this is in the singular, so it would be silly to have a primary physician, a secondary or tertiary physician all for an issue.
Why don't you sit down and write a specification of what you want then post it here and see what would possibly be the best way for you to proceed, I am almost 100% sure whatever is suggested would not include 400+ forms and multiple tables with the same 10 fields in them.
Who knows you may just learn something.
And yes it is a really nice day here..
Sam HobbsPosted Mar 15, 2010, 8:58 PM
So anyway, on to the question of getting the specific field. Is "Name" the field you want? If so, then as theLizard is saying (I think), that field is not in the SQL Select. If that is not the problem, then I think we need to know what ReadRecord is; how the class is implemented. Is it drived from DataTableReader, DbDataReader, SqlDataReader, OracleDataReader, OleDbDataReader, OdbcDataReader, IDataRecord or something else? We also need to know what the error is.
Assuming that "Name" is a field in the results, I think you can use ReadRecord.Item("Name") or maybe ReadRecord["Name"].
GustavoPosted Mar 15, 2010, 8:39 PM
To answer your questions:
1) Do I think you guys are 'fools' (that is your word). NO, I don't think that at all. Many people on this form have helped me a lot. Even you at times.
2) How old and I? What if I was to tell you I am 11 years old. What if I was 81 years old? What does that have to do with my questions?
3) I have created the select with "Name" after I found that I missed it. It still does not work.
4) Am I playing games. NO...
In conclusion: I think you have an issue. Please see your primary physician instead of taking it out on us.
Thank you and have a nice day.
theLizardPosted Mar 15, 2010, 8:02 PM
textBoxName.Text = ReadRecord.GetString(ReadRecord.GetOrdinal("Name"));
What is wrong with this, I can see it a mile away and not only can I see it a mile away, you can NEVER get what you are asking for.
I wonder why that might be.
Lets see, you have created a select statement, you are asking for a name by it's ordinal position but you have not included the name in your select statement so the ordinal position does not exist but you are also asking for the ordinal by the field name.
I don't know about the rest of the people trying to help but I think you are playing games.