Click here to Skip to main content
15,917,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings..

Yesterday I posted a code for loading method where I was looking for help and thanks for everyone who contributed...

Let me take you back from the code that i posted while looking for help...

1. Below it is my code for loading method to the form
-this is the code I posted when I was stuck...

C#
private void formloaddata() 

{ 
  SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings    ["ConnectionString01"].ConnectionString); 
string strquery = "Select UserID,Username,Password,Fullname,Surname,Email Address,Country,Province"; 
SqlDataReader read = new SqlDataReader(conn, strquery); GridView1.DataBind(); 
} 



2. This is the solution I got yesterday from one of the site members.
-solution I got was having some erros but I managed to correct them and modify the code

C#
private void formloaddata() 

{ 
SqlDataReader read SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);

conn.open //Open your Connection 
string strquery = "Select UserID,Username,Password,Fullname,Surname,Email Address,Country,Province from "Write your TableName""; 
SqlCommand cmd=new SqlCommand(strquery,conn); 
read = cmd.ExecutenonQuery(); 
if read.hasrows 
{ 
read.Read(); textbox1.text=read("UserID"); // assign this reader to your respective TextBoxes textbox2.text=read("Username"); 
textbox3.text=read("Password"); 
textbox4.text=read("Fullname"); 
textbox5.text=read("Surname"); 
textbox6.text=read("Email "); 
textbox7.text=read("Address"); 
textbox8.text=read("Country"); 
textbox9.text=read("Province"); 
} 
}



3.After the above code, I managed to rectify the mistakes and modify the code
-I have modified the code and it gives me errors

-I have commented on the errors so that you can see the errors on the codes

Erros that it gives,

*//Error :Cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'

*//Errors :for code below 'read' is a 'variable' but is used like a 'method'

Find my code below;

C#
private void formloaddata()
    {
        SqlDataReader read;
        SqlCommand comm = new SqlCommand(ConfigurationManager.ConnectionStrings["ConnectionString01"].ConnectionString);
        conn.Open();
        string strquery = "select UserID,Username,Password,Fullname,Surname,Email Address,Country,Province from tblreg";
        SqlCommand comd = new SqlCommand(strquery, conn);

*//Error: Cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'
C#
read = comd.ExecuteNonQuery();

if(read.HasRows)
{
    while (read.Read())
    {

*//Error: For code below 'read' is a 'variable' but is used like a 'method'
C#
        txtuser.Text = read("Username");
        txtPass.Text = read("Password");
        txtFulln.Text = read("Fullname");
        txtSurname.Text = read("Surname");
        txtemail.Text = read("Email Address");
        dropCountry.Text = read("Country");
        dropProvince.Text = read("province");

    }

}

try
{
    conn.Close();

}
catch (Exception ex)
{
    Response.Write("Error");
}
finally
{
    Response.Write("Executed");
}



1. Can you please correct me on those erros so that next time I will now the reason.
2. Can you please check my try catch method if it is fine..

Thanks for your help..
Posted
Updated 15-May-13 1:57am
v3
Comments
[no name] 15-May-13 7:36am    
read = comd.ExecuteNonQuery(); should be read = comd.ExecuteReader();
Member 10010198 15-May-13 7:49am    
thanks i have changed

You have used round brackets throughout: '(' and ')'
These indicate that the identifier to the left is a Method, and should be called, rather than an index which should be accessed. Use square brackets instead: '[' and ']'
C#
txtuser.Text = read["Username"];
txtPass.Text = read["Password"];
txtFulln.Text = read["Fullname"];
txtSurname.Text = read["Surname"];
txtemail.Text = read["Email Address"];
dropCountry.Text = read["Country"];
dropProvince.Text = read["province"];
But you will also need to cast the object appropriately as well:

C#
txtuser.Text = (string) read["Username"];
And so forth.
 
Share this answer
 
Comments
Member 10010198 15-May-13 7:51am    
Hi,

i have changed to txtuser.Text = read["Username"]; but it still gives errors or highlighting to shows that it is wrong

and lastly......i dont understand the code below
OriginalGriff 15-May-13 8:01am    
You need to cast it, because it is an object - you need to cast it to a string because C# is strongly typed and won't do it for you (thankfully). SqlDataReaders as a collection of objects, because the data types are not known until you execute the code at run time.

But you shouldn't store passwords in text anyway. When you are a little more familiar with C#, have a look at this:
http://www.codeproject.com/Tips/186585/Password-Storage-How-to-do-it.aspx
But don't expect it to make a lot of sense at the moment! :laugh:
Member 10010198 15-May-13 8:04am    
lol....thanks man, i am traying to train myself
OriginalGriff 15-May-13 8:09am    
I would strongly recommend you get a course, or a book, and follow it.
You seem to be trying to learn by randpomly picking stuff up and using it, which is really not a good idea. The trouble is that you miss so much stuff which later bits assume you know pretty well.
A book or course presents in in s structured way, building on previous knowldge so you shouldn't miss bit that would save you a lot of time later.
Member 10010198 15-May-13 9:28am    
thanks......i am looking at examples not copy n paste....remember that i am a begginer
you have to use
C#
read = comd.ExecuteReader();


after that change code like

C#
txtuser.Text = read["Username"].ToString();

//in below codes you can use based on your datatype.
if it is int datatype then convert to int32.
C#
txtPass.Text = read["Password"];
txtFulln.Text = read["Fullname");
txtSurname.Text = read["Surname"];
txtemail.Text = read["Email Address"];
dropCountry.Text = read["Country"];
dropProvince.Text = read["province"];
 
Share this answer
 
Comments
Member 10010198 15-May-13 7:55am    
so i must convert txtPass.Text = read["Password"]; because it is a int?
josh-jw 15-May-13 7:59am    
yes.
Member 10010198 15-May-13 8:08am    
like this....

string pass = Convert.ToString(txtPass.Text);
josh-jw 15-May-13 8:11am    
yes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900