| |

VerySource

 Forgot password?
 Register
Search
Author: raofeng76

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

[Copy link]

0

Threads

16

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-31 11:18:01
| Show all posts
cmd = new SqlCommand ("select * from users where userID =" + userID + "", cnn);

There is a syntax error!

Missing quotes !!!

cmd = new SqlCommand ("select * from users where userID = '" + userID + "'", cnn);
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-1-31 14:54:01
| Show all posts
private static void ReadOrderData (string connectionString)
{
    string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders"; // Query statement
    string str1 = "";
    string str2 = "";
    using (SqlConnection connection = new SqlConnection (connectionString))
    {
        SqlCommand command = new SqlCommand (queryString, connection); // Construct Command
        connection.Open (); // Open the connection

        SqlDataReader reader = command.ExecuteReader (); // Generate DataReader

        if (reader.Read ()) // Read data sequentially in a loop
        {
// Assign data
            str1 = reader [0] .ToString ();
            str2 = reader [1] .ToString ();
        }
        reader.Close (); // Close Reader
    }
}
Reply

Use magic Report

1

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-31 15:18:01
| Show all posts
Depressed, something new is wrong!

An invalid read attempt was made without any data.
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.InvalidOperationException: An invalid read attempt was made without any data.

Source error:


Line 19: SqlDataReader dr = cmd.ExecuteReader (CommandBehavior.CloseConnection);
Line 20: dr.Read ();
Line 21: Label1.Text = dr.GetString (0);
Line 22: TextBox1.Text = dr.GetString (1);
Line 23: TextBox2.Text = dr.GetString (2);
Reply

Use magic Report

0

Threads

16

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-31 21:00:01
| Show all posts
One thing written in the past, a piece of code:

public void table_fill ()
{
SqlConnection conn = new SqlConnection ("data source = fish_in_sky; database = MIS; uid = sa; pwd =");
SqlCommand myCommand = new SqlCommand ();
conn.Open ();
string strsql = "select real name, ID number, address, phone, Email, notes from user_info where username = '" + Request.QueryString ["name"] + "'";
SqlCommand cmd = new SqlCommand (strsql, conn);
SqlDataReader rd = cmd.ExecuteReader ();
if (rd.Read ())
{
TextBox4.Text = rd.GetValue (0) .ToString (). Trim ();
TextBox5.Text = rd.GetValue (1) .ToString (). Trim ();
TextBox6.Text = rd.GetValue (2) .ToString (). Trim ();
TextBox7.Text = rd.GetValue (3) .ToString (). Trim ();
TextBox8.Text = rd.GetValue (4) .ToString (). Trim ();
TextBox9.Text = rd.GetValue (5) .ToString (). Trim ();
}
conn.Close ();
}
Reply

Use magic Report

1

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-1 02:36:01
| Show all posts
Thank you everyone! Everything is solved!
The key to this thing: Label1.Text = dr.GetValue (0) .ToString (). Trim ();
If it is Label1.Text = dr.GetString (0), the following error will occur:
The specified conversion is invalid.
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.InvalidCastException: The specified conversion is invalid.

Thank you again!
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