Click here to Skip to main content
15,888,271 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I receive the result of a select statement in a variable and how can I divide the result into more than one variable if I am select more than one variable.
Posted
Updated 21-Apr-11 2:33am
v2

Try:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM PurchaseTypes", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }


From the OP:
sql = "select amount from data where password='" + txtpassword.Text + "'";
cmd = new SqlCommand(sql, con);
if (rdbAdd.Checked == true)
{
    if (txtpassword.Text.Length == 7)
    {
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        try
        {
            reader.Read();
            amount=(int)reader["Amount"];


That is not the same: you do not check the return result of reader.Read() - if you have no data, it will return false and you will get an error if you try to use it, as you have seen.

In addition, do not concatenate strings to form dynamic SQL: you leave yourself wide open to an accidental or deliberate SQL Injection attack, which could destroy your database. Use parametrized queries instead:
sql = "select amount from data where password=@PW";
cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@PW", txtpassword.Text);


Oh, and don't store passwords in clear in your database: major security risk! :laugh:
 
Share this answer
 
v2
Comments
Mohammed Ahmed Gouda 21-Apr-11 8:50am    
it gives me error cause there is no data in table how can i avoid this error ?
OriginalGriff 21-Apr-11 8:54am    
It was an example: you would have to change the column names "iD" and "description" to match the content of your table. And change the table name from "PurchaseTypes" to whatever you called your table...
Since I can't see your screen, I can't use your table name and column details!
Mohammed Ahmed Gouda 21-Apr-11 8:59am    
i know i changed it :)
Mohammed Ahmed Gouda 21-Apr-11 9:01am    
this is the error : Invalid attempt to read when no data is present.
OriginalGriff 21-Apr-11 9:09am    
Are you sure you got the code exactly as I did?
Please post the code you used...
Please go through with this Article

Database Programing
 
Share this answer
 
You can return the data into a DataTable by using a DataAdapter and then iterate over each DataRow returned using the Cells property. Example[^].

You can also use a DataReader and read the returned rows one by one and index into the row for the column required. Example[^].
 
Share this answer
 

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