Results 1 to 8 of 8

Thread: Problem with database??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70

    Problem with database??

    The following code doesn't retrieve the rows infact there are rows present in that. Can anyone help me out with this.
    Ur help will be appreciated.

    VB Code:
    1. OleDbConnection conn = new OleDbConnection(pathinfo);
    2.     String SQL,dr;
    3.     void Page_Load()
    4.     {
    5.         Page.DataBind();
    6.         SQL="Select Sum(Poamount) as amount from stats where Buyer='Brad' group by Poamount";
    7.         dbCommand1 = new OleDbCommand(SQL,conn);
    8.         conn.Open();
    9.         OleDbDataReader dataRead = dbCommand1.ExecuteReader();
    10.         dr = dataRead.GetString(dataRead.GetOrdinal("amount"));
    11. //      dr = dataRead.GetInt32(0);
    12.         dataRead.Close();
    13.         conn.Close();
    14.     }

    The error says there are no rows/columns.

    Thanks a lot

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    You need to call dataRead.Read() to get to the first row. If you're only bringing back a single row with a single column, I'd use ExecuteScalar:
    PHP Code:
    void Page_Load(object senderSystem.EventArgs e)
    {
        
    double amount 0.0;
        
    string cmdText "select sum ( poamount ) as amount from [stats] where buyer = 'Brad' group by poamount";
        
    string connString "provider=sqloledb;user id=sa;password=sa;database=Scratch;server=DeathAngel;";
        
    using (OleDbConnection cn = new OleDbConnection(connString))
        
    using (OleDbCommand cmd = new OleDbCommand(cmdTextcn))
        {
            try
            {
                
    cmd.Connection.Open();
                
    object result cmd.ExecuteScalar();
                if (
    result != null)
                {
                    
    amount System.Convert.ToDouble(result);
                }
            }
            finally
            {
                if (
    cmd != null && cmd.Connection != null)
                {
                    
    cmd.Connection.Close();
                }
            }
        }


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70

    But the amount is not displaying correctlyin my field

    VB Code:
    1. <%@ Page Language="C#" Debug="true"%>
    2. <%@ Import Namespace="System.Data"%>
    3. <%@ Import Namespace="System.Data.OleDb"%>
    4.  
    5.  
    6. <SCRIPT LANGUAGE="C#" Runat="server">
    7.     OleDbCommand cmd;
    8.     static String pathinfo="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/InetPub/wwwroot/database/buyerStats.mdb";
    9.     OleDbConnection conn = new OleDbConnection(pathinfo);
    10.     int dr;
    11.     void Page_Load(object sender, System.EventArgs e)
    12.     {
    13.     double amount = 0.0;
    14.     string cmdText = "select sum ( poamount ) as amount from [stats] where buyer = 'Brad' group by poamount";
    15.     using (cmd = new OleDbCommand(cmdText, conn))
    16.     {
    17.         try
    18.         {
    19.             cmd.Connection.Open();
    20.             object result = cmd.ExecuteScalar();
    21.             if (result != null)
    22.             {
    23.                 amount = System.Convert.ToDouble(result);
    24.             }
    25.         }
    26.         finally
    27.         {
    28.             if (cmd != null && cmd.Connection != null)
    29.             {
    30.                 cmd.Connection.Close();
    31.             }
    32.         }
    33.     }
    34.     conn.Close();
    35.     }
    36. </SCRIPT>
    37. <html>
    38. <body>
    39. <form runat="server">
    40.     Number of Pos:<asp:Literal id="amount" runat="server" />
    41. </form>
    42. </body>
    43. </html>

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70

    Is there anyproblem with the following code???

    It says The data value could not be converted for reasons other than sign mismatch or data overflow. For example, the data was corrupted in the data store but the row was still retrievable at output[i] = dataRead.GetInt32(0).

    VB Code:
    1. <%@ Page Language="C#" Debug="true"%>
    2. <%@ Import Namespace="System.Data"%>
    3. <%@ Import Namespace="System.Data.OleDb"%>
    4.  
    5.  
    6. <SCRIPT LANGUAGE="C#" Runat="server">
    7.     OleDbCommand cmd;
    8.     OleDbDataAdapter da1;
    9.     DataSet ds;
    10.     static String pathinfo="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/InetPub/wwwroot/database/buyerStats.mdb";
    11.     OleDbConnection conn = new OleDbConnection(pathinfo);
    12.     int dr,i=0;
    13.     int[] output;
    14.     void Page_Load(object sender, System.EventArgs e)
    15.     {
    16.     double amount = 0.0;
    17.     string cmdText = "select sum ( poamount ) as bradAmount from [stats] where buyer = 'Brad' group by Buyer";
    18.     cmdText += " UNION select sum( poamount ) as judyAmount from [stats] where buyer = 'Judy' group by Buyer";
    19.     cmdText += " UNION select sum( poamount ) as mikeAmount from [stats] where buyer = 'Mike' group by Buyer";
    20.     cmdText += " UNION select sum( poamount ) as sueAmount  from [stats] where buyer = 'Sue' group by Buyer";
    21.     cmdText += " UNION select sum( poamount ) as jeffAmount from [stats] where buyer = 'Jeff' group by Buyer";
    22.     OleDbDataReader dataRead;
    23.     using (cmd = new OleDbCommand(cmdText, conn))
    24.     {
    25.         conn.Open();
    26.         dataRead = cmd.ExecuteReader();
    27.         try
    28.         {
    29.         while(dataRead.Read())
    30.         {
    31.             output[i]=dataRead.GetInt32(0);
    32.             i++;
    33.         }        
    34.         }
    35.         finally
    36.         {
    37.             if (dataRead != null)
    38.             {
    39.                 dataRead.Close();
    40.             }
    41.             if (conn.State == ConnectionState.Open)
    42.             {
    43.                 conn.Close();
    44.             }
    45.         }
    46.     }
    47.     conn.Close();
    48.     }
    49. </SCRIPT>
    50. <html>
    51. <body>
    52.  
    53. </body>
    54. </html>

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    try something like this:
    PHP Code:
    <%@ Import Namespace="System.Data" %>
    <%@ 
    Import Namespace="System.Data.OleDb" %>
    <
    script language="C#" runat="server">
    protected 
    override void OnLoad(System.EventArgs e)
    {
        
    double amount 0.0;
        
    string mdbPath = @"C:\Documents and Settings\ccarter\My Documents\buyerStats.mdb";
        
    string connString String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}"mdbPath);
        
    string cmdText "select sum(amount) from ( select sum(poamount) as amount from stats group by buyer )";
        
    using (OleDbConnection cn = new OleDbConnection(connString))
        
    using (OleDbCommand cmd = new OleDbCommand(cmdTextcn))
        {
            try
            {
                
    cmd.Connection.Open();
                
    object result cmd.ExecuteScalar();
                if (
    result != null)
                {
                    
    amount System.Convert.ToDouble(result);
                }
            }
            finally
            {
                if(
    cmd != null && cmd.Connection != null)
                {
                    
    cmd.Connection.Close();
                }
            }
        }
        
    lblAmount.Text amount.ToString("c");
    }
    </
    script>
    <
    html>
        <
    body>
            <
    form runat="server">
                
    Amount:<asp:Label ID="lblAmount" Runat="server"/>        
            </
    form>
        </
    body>
    </
    html

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70
    I dont want to add all the buyers at a time
    I want to add each buyers sum in to a seperate variable

  7. #7
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    PHP Code:
    <%@ Import Namespace="System.Collections" %>
    <%@ 
    Import Namespace="System.Data" %>
    <%@ 
    Import Namespace="System.Data.OleDb" %>
    <
    script language="C#" runat="server">
    private 
    struct Buyer
    {
        public 
    double Amount;
        public 
    string Name;
    }
    protected 
    override void OnLoad(System.EventArgs e)
    {
        
    ArrayList buyerList = new ArrayList();
        
    string mdbPath = @"C:\Documents and Settings\ccarter\My Documents\buyerStats.mdb";
        
    string connString String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}"mdbPath);
        
    string cmdText "select buyer, sum(poamount) as amount from stats group by buyer";
        
    using (OleDbConnection cn = new OleDbConnection(connString))
        
    using (OleDbCommand cmd = new OleDbCommand(cmdTextcn))
        {
            
    OleDbDataReader dr null;
            try
            {
                
    cmd.Connection.Open();
                
    dr cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (
    dr != null)
                {
                    while(
    dr.Read())
                    {
                        
    Buyer buyer = new Buyer();
                        
    buyer.Name dr["buyer"].ToString();
                        
    buyer.Amount System.Convert.ToDouble(dr["amount"]);
                        
    buyerList.Add(buyer);
                    }
                }
            }
            finally
            {
                if (
    dr != null)
                {
                    
    dr.Close();
                }
                if(
    cmd != null && cmd.Connection != null)
                {
                    
    cmd.Connection.Close();
                }
            }
            foreach(
    Buyer buyer in buyerList)
            {
                
    lblAmount.Text += buyer.Name " -> " buyer.Amount.ToString("c") + "<br/>";
            }
        }
        
    }
    </
    script>
    <
    html>
        <
    body>
            <
    form runat="server">
                
    Amounts:<br/><asp:Label ID="lblAmount" Runat="server"/>        
            </
    form>
        </
    body>
    </
    html

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    michigan
    Posts
    70
    The problem is with sum() command in sql.
    I donno why its saysis data conversion problem.
    If I use count() command instead of sum() it was working fine.

    Thanx a lot for ur replies pvb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width