| |

VerySource

 Forgot password?
 Register
Search
View: 1359|Reply: 14

Regarding the problem that the return value of SqlDataReader cannot be assigned to a variable in asp.NET, anxious, pleas

[Copy link]

1

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-21 09:00:01
| Show all posts |Read mode
The script source code is as follows:
<script runat = "server">
    string strcnn = "server = localhost; uid = sa; pwd = wbq; database = WebDate_WBQ";
    SqlConnection cnn;
    SqlCommand cmd;

    void Page_Load (object sender, EventArgs e) {
       string userID = Request.QueryString ["userID"];
       cnn = new SqlConnection (strcnn);
       cnn.Open ();
       cmd = new SqlCommand ("select * from users where userID =" + userID + "", cnn);
       SqlDataReader dr = cmd.ExecuteReader ();
       Label1.Text = dr ("userID"). ToString (); // The following code is wrong.
       TextBox1.Text = dr ("userName"). ToString ();
       TextBox2.Text = dr ("pwd"). ToString ();
       TextBox3.Text = dr ("sex"). ToString ();
       TextBox4.Text = dr ("regDate"). ToString ();
       TextBox5.Text = dr ("QQ"). ToString ();
       TextBox6.Text = dr ("email"). ToString ();
       TextBox7.Text = dr ("address"). ToString ();
       dr.Close ();
       cnn.Close ();
    }
</ script>

The compilation error message is as follows:
Compile Error
Explanation: An error occurred during the compilation of the resources required to service the request. Please check the following specific error details and modify the source code appropriately.
Compiler Error Message: CS0118: "dr" means "variable" and should be "method" here

Source error:

Line 18: cmd = new SqlCommand ("select * from users where userID =" + userID + "", cnn);
Line 19: SqlDataReader dr = cmd.ExecuteReader ();
Line 20: Label1.Text = dr ("userID"). ToString ();
Line 21: TextBox1.Text = dr ("userName"). ToString ();
Line 22: TextBox2.Text = dr ("pwd"). ToString ();
Reply

Use magic Report

1

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-30 13:09:01
| Show all posts
Urgent use!
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-1-30 14:00:01
| Show all posts
SqlDataReader dr = cmd.ExecuteReader ();
Line 20: Label1.Text = dr ("userID"). ToString ();
Line 21: TextBox1.Text = dr ("userName"). ToString ();
Line 22: TextBox2.Text = dr ("pwd"). ToString ();

Add a sentence before line 20
dr.Read ();
Reply

Use magic Report

1

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-30 17:45:02
| Show all posts
Dude, add a sentence before line 20: dr.Read (); It doesn't work, it's still the original mistake!
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-1-30 19:09:01
| Show all posts
dr ["userID"]. ToString ();
()-> []
Reply

Use magic Report

1

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Unknown

 Author| Post time: 2020-1-30 20:54:01
| Show all posts
---- dr ["userID"]. ToString ();
()-> []

Is this wrong?

I changed it to: Lable1.Text = (string) .dr ["userID"] is not right!
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-1-30 21:54:02
| Show all posts
sqlCon.Open ();
            SqlDataReader sqlReader = sqlCmd.ExecuteReader (CommandBehavior.CloseConnection);
            string [] strCourses = new string [20];
            string [] iValues ​​= new string [20];
            int i = 0;
            while (sqlReader.Read ())
            {
                iValues ​​[i] = sqlReader.GetString (0);
                strCourses [i] = sqlReader.GetString (1);
                i ++;
            }
            sqlReader.Close ();

This is an example
Reply

Use magic Report

1

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-31 06:45:01
| Show all posts
I changed to:
       Label1.Text = dr.GetString (0);
       TextBox1.Text = dr.GetString (1);
       TextBox2.Text = dr.GetString (2);
       TextBox3.Text = dr.GetString (3);
       TextBox4.Text = dr.GetString (4);
       TextBox5.Text = dr.GetString (5);
       TextBox6.Text = dr.GetString (6);
       TextBox7.Text = dr.GetString (7);
After the original error disappeared

SqlDataReader sqlReader = sqlCmd.ExecuteReader (CommandBehavior.CloseConnection);
What does the code in brackets mean?

Why do i change myself to
SqlDataReader dr = cmd.ExecuteReader (CommandBehavior.CloseConnection);
It appeared again:

Line 1: Syntax error near '='.
Explanation: An unhandled exception occurred during the execution of the current web request. Check the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Line 1: There was a syntax error near '='.

If you change it to: SqlDataReader dr = cmd.ExecuteReader (); the same error!
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-1-31 09:27:01
| Show all posts
Boss, will it work?
Reply

Use magic Report

1

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-31 10:09:01
| Show all posts
Dude, I have n’t learned that much. I am doing homework now!
Could you tell me more about it?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list