Values not being passed correctly

First I will preface this by saying this is C# code. I have had very good help with this forum and came here for help.

The issue I am having is the values from are not being passed into my array correctly. I've done a Response.Write to check and I have the correct data. I can not seem to get this to pass into my array or the database correctly.My goal is to pass ID and P into an array and in the for loop through them then output the corresponding data. What is showing up for P is 0. I am using razor C# without MVC.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
else if (Page == "2")
{
    int foo;
    string paramName = "";


    using (SqlConnection connect = new SqlConnection(conn.strConn))
    {
        connect.Open();
        using (SqlCommand comm = new SqlCommand(conn.SQL, connect))
        {
            using (SqlDataReader reader = comm.ExecuteReader())
            {
                reader.Read();
                foo = Convert.ToInt32(reader["bar"]);
            }
        }
    }

    string[] ID = new string[foo];
    double[] P  = new double[foo];

    i = 0;

    if (!HttpContext.Current.Request["bar"].IsEmpty())
    {
        foreach (var selection in HttpContext.Current.Request["bar"].Split(','))
        {
            IDs[i] = selection.ToString();
            ID = selection.ToString();
            paramName = "P" + ID;
            Ps[i] = Convert.ToDouble(HttpContext.Current.Request[paramName]);
            P = Convert.ToDouble(HttpContext.Current.Request[paramName]);
            i++;

            //insert into Table
            conn.SQL = "Insert Into Table (ID, Date, Number ) Values (@ID, @Dt, @Nm)";

            using (SqlConnection connection = new SqlConnection(conn.strConn))
            {
                connection.Open();
                using (SqlCommand comm = new SqlCommand(conn.SQL, connection))
                {

                    comm.Parameters.AddWithValue("@ID", ID);
                    comm.Parameters.AddWithValue("@Dt", DateTime.Now);
                    comm.Parameters.AddWithValue("@Nm", Math.Round(P, 2));


                    comm.ExecuteNonQuery();
                }
            }
        }  
            conn.SQL = "Select Name, Name1 From Table2 Where ID = @ID";

        <table border="0">
            <tr class="d0">
                <td width="250px"><b><u>Name</u></b></td>
                <td><b><u>Data</u></b></td>
            </tr>
            @for (int x = 0; x < i; x++)
            {
                using (SqlConnection connection = new SqlConnection(conn.strConn))
                {
                    connection.Open();
                    using (SqlCommand comm = new SqlCommand(conn.SQL, connection))
                    {
                        comm.Parameters.AddWithValue("@ID", IDs[x]);
                        using (SqlDataReader reader = comm.ExecuteReader())
                        {
                            reader.Read();

                            <tr class="d@((x+1)%2)">
                                <td>@(reader["Name"] + " " + reader["Name1"])</td>
                                <td>@Ps[x].ToString("c")</td>

                            </tr>
                            //Response.Write(@Ps[x]);
                        }
                    }
                }
            }
        </table>

    }
}
My goal is to pass ID and P into an array and in the for loop through them


But ID and P are arrays. How do you want to pass an array into an array?

Some thoughts (I don't know what what libraries you're using, so this is just from a general point of view):

foo = Convert.ToInt32(reader["bar"]);
Are you sure reader["bar"] will be non-zero? If it's zero, the size of your arrays will be 0.

1
2
            IDs[i] = selection.ToString();
            ID = selection.ToString();


What is IDs? ID is defined as an array of string. It looks like you're trying to assign a single string to an array of strings. You seem to have a type mismatch. Do you see why this is wrong?

1
2
            Ps[i] = Convert.ToDouble(HttpContext.Current.Request[paramName]);
            P = Convert.ToDouble(HttpContext.Current.Request[paramName]);


Same possible issue here. What is the purpose of the Ps array? What type is the Ps array?



Last edited on
Thats where im getting confused.

<td>@Ps[x].ToString("c")</td> is what is being displayed to the screen. this is where the zero is. IDs and Ps are coming from a SQL db.

the really weird part is that when I response.write with variables from "page1"
1
2
Response.Write(HttpContext.Current.Request["D"] + "<br>");
Response.Write(HttpContext.Current.Request["DP"] + "<br>");


they both contain the information I need. I've tired using those but receive various of errors about "cant convert double to int, cant convert double to string, cant convert double to double"
cant convert double to int, cant convert double to string, cant convert double to double

The exact error message would probably be helpful. It doesn't sound like you're giving the error message verbatim. If I had to guess, I'd assume Ps was an array of doubles? It sounds like you have an array of doubles, but you're trying to convert that into a double, or vice versa (trying to convert a double to an array of doubles).

I'm not familiar with razor C#, so my help may be kinda useless, so I might just call it quits, hopefully someone else can help (but note that since this is a C++ forum, might not be likely).

Anyway, few last questions:
ToString("c") is trying to format the double as a currency value, correct? Perhaps the value is too small, and it's rounding the value to 0?
What type is Ps? What is the actual value of Ps[x] before you call Ps[x].ToString("c")?


ex, the following would display 0.00 because it's too small of an amount.
1
2
3
4
5
6
7
8
    public class Program
    {
        public static void Main(string[] args)
        {
            double v = 0.000125;
            Console.WriteLine(v.ToString("C"));
        }
    }


The following, however, will fail because it can't convert an array of doubles to formatted string
1
2
3
4
5
6
7
8
        public static void Main(string[] args)
        {
            //Your code goes here
            double v = 0.000125;
            double[] varr = {0.001, 0.02, 0.5, 1.5};
            Console.WriteLine(v.ToString("C"));
            Console.WriteLine(varr.ToString("C")); // fails
        }

Note how v is a double, but varr is an array of doubles.
(I'm not sure if that's what your problem is, just guessing).
Last edited on
sorry No that was not verbatim. this is the error i am receiving not that I have

I made a few changes and these are the new errors verbatim

CS0029: Cannot implicitly convert type 'string' to 'string[]'
1
2
3
4
5
6
IDs = selection.ToString();
            ID[i]selection.ToString();
            paramName = "P" + ID;
            Ps = Convert.ToDouble(HttpContext.Current.Request[paramName]);
            P[i] = Convert.ToDouble(HttpContext.Current.Request[paramName]);
            i++;


and this error with this change
Property or indexer 'string.this[int]' cannot be assigned to -- it is read only

1
2
3
4
5
6
 IDs[i] = selection.ToString();
            ID[i] = selection.ToString();
            paramName = "P" + ID;
            Ps[i] = Convert.ToDouble(HttpContext.Current.Request[paramName]);
            P[i] = Convert.ToDouble(HttpContext.Current.Request[paramName]);
            i++;
I think you're getting confused on the the difference between a string type and an array of strings.

_______________________

I assume IDs is an array of strings.
IDs = selection.ToString(); won't work because you are trying to assign a individual string to an array of strings.

I think you're trying to do the equivalent of this:
1
2
string[] string_arr = {"hello", "there", "blah"};
string_arr = "goodbye";

You can't do this.

______________________

ID[i]selection.ToString();
No idea what this is. Looks like a typo.

__________________________________________

paramName = "P" + ID
ID is an array of strings. You are trying to concatenate (add together) a string with an array of strings. This is not possible.

Arrays must be assigned to one index at a time!

Did you mean:
paramName = "P" + ID[i];?

_____________________________

Property or indexer 'string.this[int]' cannot be assigned to -- it is read only

I think that error message means you're trying to assign a value to an individual character of the string. Strings are immutable in C#, this can't be done.

1
2
3
4
string[] s_arr = {"Hello", "Howdy", "Hi"};
s_arr; // this is an array of strings
s_arr[0]; // this is the first string, "Hello"
s_arr[0][1]; // this is the 2nd character of the 1st string, 'e'. It is immutable. 


_____________________________

What type is ID? What type is IDs?
Last edited on
Ok im getting some better understanding.

1
2
 string[] IDs = new string[foo];
    double[] Ps  = new double[foo];

these are the array types
ID and P
don't have types they are just using paramName and yes the quotes are a typo
Last edited on
It looks a bit too compilcated for my taste. Is there a reason why you don't use a SqlDataSource and DataGrid or Repeater control?
Thank you for all of your help. I was able to figure it out. The problem is higher up. thanks again
Topic archived. No new replies allowed.