I couldn't find an answer for this questions no matter how much I tried. What I have so far is this:
The array of data is gathered and displayed with
readonly string[] values;
protected void btnAddClick(object sender, EventArgs e)
{
List values = new List
{
txtActivitate.Text.ToString()
};
for (int i = 0; i < values.Count; i++)
{
Message.Text += values[i] + "
";
}
}
and is working properly. Then I try a select with
protected void btnSelectClick(object sender, EventArgs e)
{
foreach (string val in values)
{
using (SqlConnection conn = new SqlConnection(connString))
{
string sqlQuery = "SELECT * FROM tblActivitate t1 JOIN tblEvaris t2 ON t1.AID = t2.AID JOIN tblMasura t3 on t2.EID = t3.EID WHERE t1.Activitate = @Activitate ORDER BY Componenta, FactorRisc, FormaManifestare";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Activitate", val);
SqlDataAdapter da = new SqlDataAdapter(cmd);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
conn.Close();
}
}
}
}
and I got error
System.NullReferenceException: 'Object reference not set to an instance of an object.' Local0 was null.
How it should be done?

Prasad RaveendranPosted Oct 31, 2023, 3:51 AM
Here's an updated version of your code to address the problem:
Changes made:
In the
Page_Loadmethod, it checks if the session variableMyListis null. If it's null, a new list is created and assigned to the session variable. If it's not null, it retrieves the existing list from the session.In the
btnAddClickmethod, it retrieves the list from the session, adds the new value to this list, updates the session variable with the modified list, and then displays all the values in the updated list.This should prevent the duplication of values when the button is clicked repeatedly. Now, the list is properly retrieved from the session, and new values are added to it without doubling the existing values.
Marius VasilePosted Oct 31, 2023, 5:40 PM
Got it work with
Marius VasilePosted Oct 31, 2023, 4:50 PM
Prasad, I tried your solution but it still double the data in List values and Session["MyList"]. I have to find a way to prevent adding doubled values, tried with
but is not working, no data is saved
Marius VasilePosted Oct 31, 2023, 5:47 AM
Thank you Prasad, I was trying to declare a public array of strings, populated with first method and use valies in the second. With the Session I tried to preserve values in the list and use it in the second method. Most probably values were not preserved due to post back. I try later today your solution but most likely will work
Prasad RaveendranPosted Oct 31, 2023, 3:49 AM
It seems like you've declared two separate variables named
valueswithin the same scope, causing confusion between the class-level variable and the local variable declared inside thebtnAddClickmethod. The issue arises because the local variable inside the method is hiding the class-level variable.To resolve the "empty value" problem, you can differentiate between the local variable and the class-level variable by using different names. Here's the corrected code:
By differentiating the names between the class-level variable
classLevelValuesand the local variablelocalValues, you can avoid conflicts and ensure that the intended variable is used. Also, ensure that you're populating and iterating through the correct list based on your requirement (either the class-level variable or the local variable).Marius VasilePosted Oct 30, 2023, 7:41 PM
Well, I manage to have a work arround using a Session but now with every click of a button, the values added previously are doubled
What to do?
Marius VasilePosted Oct 30, 2023, 5:55 PM
I did that already but no values are passed to second method. Full code below
Jignesh KumarPosted Oct 30, 2023, 5:43 PM
Change string array to list of string. I have updated my answer.
Marius VasilePosted Oct 30, 2023, 3:50 PM
Thank you Jignesh,
I implemented your solution and I have the following error
Jignesh KumarPosted Oct 30, 2023, 9:21 AM
Please try above one it should work, you are creating new object of string array so existing values will read off.
Marius VasilePosted Oct 29, 2023, 1:06 PM
The problem I have is that string values is not passed to select query. I modified a little the select method to
and is working. But if I try to get parameters from
I got empty values. Why?