C# ASP.NET - Get Value from DetailsView

Discussion in 'C#' started by DoubleDiamond, Mar 25, 2008.

  1. #1
    All,

    Really need some assistance, have tried with formview,detailsview,gridview.

    SQL Query returning correct data just one line, I want to get the data value for bs_bond_price.

    ASP Code as Follows

    <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="test"
    Height="50px" Style="z-index: 109; left: 8px; position: absolute; top: 474px"
    Width="125px" DataKeyNames="bs_bond_price,bs_isin,bs_issuer_id" DataMember="DefaultView" OnDataBinding="Page_Load" OnDataBound="Page_Load" OnItemCreated="Page_Load">
    </asp:DetailsView>

    C# so far

    Label1.Text = DetailsView1.Rows[1].Cells[2].Text;

    I have tried various combinations of numbers in the array fields including 0's

    I keep getting an out of range error. All the stuff I have seen on the web so far pertains to VB.NET or the threads are very unclear.

    If one of you could give me a clear concise answer and explain how it works I would really appreciate it

    Cheers
    DD
     
    DoubleDiamond, Mar 25, 2008 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    VB and C# are easily converted as they are simply formatting differences (with one or two very small exceptions such as optional variables for subs/ functions) - http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx is a free online tool to convert between the two.

    So you are trying to display a row of data from an SQL query and then show one element of it again in a label control outside of the control?

    One simple solution is to define the fields to be displayed manually and then in the bs_bond_price element have it execute a function that sets the label1 text and returns the same value back to the detailsview.

    So would be something like:

    Code behind:
    
    protected string SetLabel(string value)
     {
         Label1.Text = value;
         return value;
     }
    Code (C#):
    aspx for detailsview:
    
    <asp:Literal ID="BondPriceLT" runat="server" Text='<%# SetLabel(Eval("bs_bond_price")) %>' />
    
    Code (ASP.Net):
     
    AstarothSolutions, Mar 25, 2008 IP
  3. DoubleDiamond

    DoubleDiamond Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi

    Thanks for coming back to me however, that wasn't what I was looking for.

    I have listed the process steps below, Step3 & Step4 are not a problem.

    I need to understand how to complete Step1 & Step2
    ----------------------------------------------------------------------
    Step1, Grab the value supplied by the SQL server (the actual value for bs_bond_price)

    Step2, Stuff the value into a variable called 'a' for example

    Step3, Multiply 'a' by 'b'(value inserted by the user into the equation via a textbox)

    Step4, Produce the total in a label
    ----------------------------------------------------------------------
    If anybody can help it would b very much appreciated

    Cheers
    DD
     
    DoubleDiamond, Mar 25, 2008 IP
  4. jawahar

    jawahar Active Member

    Messages:
    1,044
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #4
    did you try

    DetailsView1.Rows[0].Cells[0].Text
     
    jawahar, Mar 25, 2008 IP
  5. DoubleDiamond

    DoubleDiamond Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yep tried it yesterday, errors on out of array.

    I am a real newbie to this. Please put into context for me by thinking of exactly what I am trying to do(given in the steps above) and then put all of the code in so I can understand how it fits together.

    really appreciate your time

    cheers
    DD
     
    DoubleDiamond, Mar 25, 2008 IP
  6. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you are only wanting to do a sum with it why on earth are you using a data control? Write the SQL and execute it to get the value and then do the maths with it.

    
     protected void CalculateValue(int B, int ProductID)
     {
         SqlConnection sqlconn = new SqlConnection("Your connection String");
         string sqlQuery = "Select bs_bond_price From Tablename Where Something=@BondID";
         SqlCommand sqlcomm = new SqlCommand(sqlQuery, sqlconn);
       
         // set parameters
         SqlParameter param = new SqlParameter("BondID", SqlDbType.Int);
         param.Value = ProductID;
         sqlcomm.Parameters.Add(param);
        
         sqlconn.Open();
         double Price = sqlcomm.ExecuteScalar;
         sqlconn.Close();
         sqlcomm.Dispose();
        
         // calculate the value and add to label
         Label1.Text = "$" + (Price * B);
    }
    Code (C#):
     
    AstarothSolutions, Mar 26, 2008 IP
  7. Arun0203

    Arun0203 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hi i'm doing a mini project for my education. topic is desktop recorder... we capture our desktop scren as a video file this s the concept and it contain video convertor and audio converter.... bt i dont know how to create problem statement and usecase specification... plz rly me
     
    Arun0203, Jan 11, 2010 IP