setting label.Text by the SqlDataReader's value
-
hey guys..i have a SqlTable which consists of 3 columns and 1 row..i have 3 labels on my form and try to change these labels' texts with the datas in my database i tried something like that
cmd = new SqlCommand("sp_Deneme", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();cmd.ExecuteReader(); dt = new DataTable(); DataRow row = dt.NewRow(); for (int i = 0; i < reader.FieldCount; i++) { dt.Columns.Add(i.ToString(), Type.GetType("System.String")); dt.Rows\[0\]\[i\] = reader\[i\].ToString(); } label1.Text = dt.Rows\[0\]\[1\].ToString(); label2.Text = dt.Rows\[0\]\[2\].ToString(); label3.Text = dt.Rows\[0\]\[3\].ToString();
but there is no change..my way is wrong ?
-
hey guys..i have a SqlTable which consists of 3 columns and 1 row..i have 3 labels on my form and try to change these labels' texts with the datas in my database i tried something like that
cmd = new SqlCommand("sp_Deneme", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();cmd.ExecuteReader(); dt = new DataTable(); DataRow row = dt.NewRow(); for (int i = 0; i < reader.FieldCount; i++) { dt.Columns.Add(i.ToString(), Type.GetType("System.String")); dt.Rows\[0\]\[i\] = reader\[i\].ToString(); } label1.Text = dt.Rows\[0\]\[1\].ToString(); label2.Text = dt.Rows\[0\]\[2\].ToString(); label3.Text = dt.Rows\[0\]\[3\].ToString();
but there is no change..my way is wrong ?
You are not getting the data. All you have done is set things up ready to get the data. You need to get the reader to do a
Read()
and in order to do that you will need to have aSqlDataReader
member to use in the call. change:cmd.ExecuteReader();
to
SqlDataReader reader = cmd.ExecuteReader(); reader.Read();
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
hey guys..i have a SqlTable which consists of 3 columns and 1 row..i have 3 labels on my form and try to change these labels' texts with the datas in my database i tried something like that
cmd = new SqlCommand("sp_Deneme", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();cmd.ExecuteReader(); dt = new DataTable(); DataRow row = dt.NewRow(); for (int i = 0; i < reader.FieldCount; i++) { dt.Columns.Add(i.ToString(), Type.GetType("System.String")); dt.Rows\[0\]\[i\] = reader\[i\].ToString(); } label1.Text = dt.Rows\[0\]\[1\].ToString(); label2.Text = dt.Rows\[0\]\[2\].ToString(); label3.Text = dt.Rows\[0\]\[3\].ToString();
but there is no change..my way is wrong ?
You didn't tell the system which procedure to execute. Why use a DataTable? Just copy the values from the DataReader to the labels. On the other hand, you shouldn't have the data access in the UI layer, but that's another matter.
modified on Saturday, December 11, 2010 12:52 PM
-
You didn't tell the system which procedure to execute. Why use a DataTable? Just copy the values from the DataReader to the labels. On the other hand, you shouldn't have the data access in the UI layer, but that's another matter.
modified on Saturday, December 11, 2010 12:52 PM
create Proc [dbo].[sp_Deneme]
as
Begin
select * from Rehber
Endhey friend..i use that procedure to get datas and i tried like that and it worked..
cmd = new SqlCommand("sp\_Deneme", conn); cmd.CommandType = System.Data.CommandType.StoredProcedure; conn.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { label1.Text = reader.GetInt32(0).ToString(); label2.Text = reader.GetString(1); label3.Text = reader.GetString(2); }
-
create Proc [dbo].[sp_Deneme]
as
Begin
select * from Rehber
Endhey friend..i use that procedure to get datas and i tried like that and it worked..
cmd = new SqlCommand("sp\_Deneme", conn); cmd.CommandType = System.Data.CommandType.StoredProcedure; conn.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { label1.Text = reader.GetInt32(0).ToString(); label2.Text = reader.GetString(1); label3.Text = reader.GetString(2); }
That's looking better.
-
That's looking better.
No comment on
SELECT *
? And what about numbered fields as inGetString(1)
? :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
No comment on
SELECT *
? And what about numbered fields as inGetString(1)
? :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc Pattyn wrote:
No comment on SELECT * ?
I use that all the time.
Luc Pattyn wrote:
GetString(1) ?
Eh, yeah, I never use the Get methods, I generally just cast to the type, but whatever; it's better than it was.
-
Luc Pattyn wrote:
No comment on SELECT * ?
I use that all the time.
Luc Pattyn wrote:
GetString(1) ?
Eh, yeah, I never use the Get methods, I generally just cast to the type, but whatever; it's better than it was.
if the fields get reordered, the code breaks. which wouldn't happen when the fields were named explicitly:
SELECT fieldName1,fieldName2,fieldName3 ...
The alternative is more cumbersome:reader.GetString(reader.GetOrdinal("fieldName2"))
:)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
if the fields get reordered, the code breaks. which wouldn't happen when the fields were named explicitly:
SELECT fieldName1,fieldName2,fieldName3 ...
The alternative is more cumbersome:reader.GetString(reader.GetOrdinal("fieldName2"))
:)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc Pattyn wrote:
if the fields get reordered
Who'd do a silly thing like that? IDataReader fields can be accessed by name too. Item [([(String ])]) Gets the column with the specified name. (Inherited from IDataRecord.) This is from the WCF experiment I was playing with last spring:
dr = this.con.ExecuteReader ( @" SELECT \* FROM JunkName WHERE Id=@Param0 " , ID ) ; if ( dr.Read() ) { result = new Junk.Types.Record() { ID = (System.Guid) dr \[ "ID" \] , LastName = (string) dr \[ "LastName" \] , FirstName = (string) dr \[ "FirstName" \] , Gender = (Junk.Types.Gender) (byte) dr \[ "Gender" \] , FavouriteColour = Colour.Parse ( (string) dr \[ "Colour" \] ) , Created = (System.DateTimeOffset) dr \[ "Created" \] } ; }
-
Luc Pattyn wrote:
if the fields get reordered
Who'd do a silly thing like that? IDataReader fields can be accessed by name too. Item [([(String ])]) Gets the column with the specified name. (Inherited from IDataRecord.) This is from the WCF experiment I was playing with last spring:
dr = this.con.ExecuteReader ( @" SELECT \* FROM JunkName WHERE Id=@Param0 " , ID ) ; if ( dr.Read() ) { result = new Junk.Types.Record() { ID = (System.Guid) dr \[ "ID" \] , LastName = (string) dr \[ "LastName" \] , FirstName = (string) dr \[ "FirstName" \] , Gender = (Junk.Types.Gender) (byte) dr \[ "Gender" \] , FavouriteColour = Colour.Parse ( (string) dr \[ "Colour" \] ) , Created = (System.DateTimeOffset) dr \[ "Created" \] } ; }
PIEBALDconsult wrote:
reordered. Who'd do a silly thing like that?
Could happen by accident, after a refactoring; or maybe a convoluted backup/restore operation.
PIEBALDconsult wrote:
IDataReader fields can be accessed by name too.
Sure, that is what you and I would do. All but referring by ordinal. So I did expect your objection to it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
PIEBALDconsult wrote:
reordered. Who'd do a silly thing like that?
Could happen by accident, after a refactoring; or maybe a convoluted backup/restore operation.
PIEBALDconsult wrote:
IDataReader fields can be accessed by name too.
Sure, that is what you and I would do. All but referring by ordinal. So I did expect your objection to it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc Pattyn wrote:
I did expect
I try to do the unexpected and not the expected -- much like the Spanish Inquisition... oh, but maybe you expected that.
-
Luc Pattyn wrote:
I did expect
I try to do the unexpected and not the expected -- much like the Spanish Inquisition... oh, but maybe you expected that.
PIEBALDconsult wrote:
maybe you expected
you'll never now. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.