Classes
-
Hi.i want to craete a class that will read data from database and display it on the texboxes.but it can not display it.so i did this try { SqlCommand cmdSearch = new SqlCommand(); cmdSearch.Connection = conn; cmdSearch.CommandText = "SELECT Name,Surname FROM Users WHERE UserName =@UserName"; SqlParameter Nam = new SqlParameter(); Nam.ParameterName = "@Name"; Nam.Direction = System.Data.ParameterDirection.Output; Nam.Value = Name; cmdSearch.Parameters.Add(Nam); SqlParameter Sur = new SqlParameter(); Sur.ParameterName="@Surname"; Sur.Direction = System.Data.ParameterDirection.Output; Sur.Value = Surname; cmdSearch.Parameters.Add(Sur); SqlParameter Use = new SqlParameter("@UserName",System.Data.SqlDbType.VarChar,50,UserName); Use.Value = UserName; cmdSearch.Parameters.Add(Use); SqlParameter Lev = new SqlParameter(); Lev.ParameterName="@Level"; Lev.Direction = System.Data.ParameterDirection.Output; Lev.Value = Surname; cmdSearch.Parameters.Add(Lev); SqlDataReader dr =cmdSearch.ExecuteReader(); if(dr.Read() == true) { MessageBox.Show("Record found"); } else { MessageBox.Show("Record not found"); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close();
-
Hi.i want to craete a class that will read data from database and display it on the texboxes.but it can not display it.so i did this try { SqlCommand cmdSearch = new SqlCommand(); cmdSearch.Connection = conn; cmdSearch.CommandText = "SELECT Name,Surname FROM Users WHERE UserName =@UserName"; SqlParameter Nam = new SqlParameter(); Nam.ParameterName = "@Name"; Nam.Direction = System.Data.ParameterDirection.Output; Nam.Value = Name; cmdSearch.Parameters.Add(Nam); SqlParameter Sur = new SqlParameter(); Sur.ParameterName="@Surname"; Sur.Direction = System.Data.ParameterDirection.Output; Sur.Value = Surname; cmdSearch.Parameters.Add(Sur); SqlParameter Use = new SqlParameter("@UserName",System.Data.SqlDbType.VarChar,50,UserName); Use.Value = UserName; cmdSearch.Parameters.Add(Use); SqlParameter Lev = new SqlParameter(); Lev.ParameterName="@Level"; Lev.Direction = System.Data.ParameterDirection.Output; Lev.Value = Surname; cmdSearch.Parameters.Add(Lev); SqlDataReader dr =cmdSearch.ExecuteReader(); if(dr.Read() == true) { MessageBox.Show("Record found"); } else { MessageBox.Show("Record not found"); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close();
A question ends with a ? BTW, never, ever, never ever, do if(dr.Read() == true) unless it is just a typo.
A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
-
A question ends with a ? BTW, never, ever, never ever, do if(dr.Read() == true) unless it is just a typo.
A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
Ennis Ray Lynch, Jr. wrote:
BTW, never, ever, never ever, do if(dr.Read() == true) unless it is just a typo.
Personally, I hate this syntax would use
if(dr.Read())
- im interested why you think this should "never ever" be done though -
Ennis Ray Lynch, Jr. wrote:
BTW, never, ever, never ever, do if(dr.Read() == true) unless it is just a typo.
Personally, I hate this syntax would use
if(dr.Read())
- im interested why you think this should "never ever" be done thoughBecause you will get flamed by coworkers :) Really it doesn't bother me too mucb but everyone else around here seems to pitch a fit about it. Of course, with a data reader it should be while(dr.Read()) :p as well
A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
-
Because you will get flamed by coworkers :) Really it doesn't bother me too mucb but everyone else around here seems to pitch a fit about it. Of course, with a data reader it should be while(dr.Read()) :p as well
A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
Ennis Ray Lynch, Jr. wrote:
it should be while(dr.Read()) :p as well
Only if you expect the datareader to have more than 1 row. If I wanted to ensure that I only ever read the first row, then
if(dr.Read())
would be a necessity! -
Ennis Ray Lynch, Jr. wrote:
it should be while(dr.Read()) :p as well
Only if you expect the datareader to have more than 1 row. If I wanted to ensure that I only ever read the first row, then
if(dr.Read())
would be a necessity!If you are using if(dr.Read()) to ensure only one row your query is bad.
A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane