list into list in javascript,c#

Hi,

I am coding a game under Unity 3D
But i have a problem
I want to print questions and respectiv answers , so I do a list and in that list I add a list
But it is not adding
There is my js code:

using UnityEngine;
using System;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;  


using System.Collections.Generic;
public class CSharp1 : MonoBehaviour 
{
	//This variable will be accessed through JavaScript
	public string message = "And this is a C# code being accessed through JavaScript.";
	
	private static MySqlConnection dbConnection;
	public Texture backGround;
	static string host = "127.0.0.1";
	static string root = "root";
	static string mdp = "";
	static string result = "";
	static int id = 0;

	
	//public List<string> l = new List<string>(); 

	public List<List<string>> Listquestion=new List<List<string>>();
		//public List<string> list1 = new List<string>();


	
	void OnGUI()
		
	{Listquestion.Clear ();
		
		Listquestion.Add (new List<String>());

			
			string connectionString = "Server="+host+";Database=projet3d;User ID="+root+";Password="+mdp+";Pooling=false";
			openSqlConnection(connectionString);
			MySqlCommand mySqlCommand = new MySqlCommand("Select * from q_chap1 ;", dbConnection);  

			MySqlDataReader reader = mySqlCommand.ExecuteReader();
			try  
			{  
				while (reader.Read())  
				{  


					if (reader.HasRows)  {


						Listquestion[id].Add (reader.GetString(1));
						id=id+1;

						Debug.Log("ok");


				} 
			}id=0;	}  
			
			catch (Exception)  
			{  
				Console.WriteLine("Failed!");  
			} 
			finally  
			{  
				reader.Close();  
			}






		MySqlCommand mySqlCommand1 = new MySqlCommand("Select * from q_choix ;", dbConnection);  
		
		MySqlDataReader reader1 = mySqlCommand.ExecuteReader();
		try  
		{  
			while (reader1.Read())  
			{  
				
				
				if (reader1.HasRows)  {
					
					
					Listquestion[id].Add (reader1.GetString(1));
					Listquestion[id].Add (reader1.GetString(2));
					Listquestion[id].Add (reader1.GetString(3));
					Listquestion[id].Add (reader1.GetString(5));
					id=id+1;
					
					Debug.Log("ok");
					
					
				} 	
			}}  
		
		catch (Exception)  
		{  
			Console.WriteLine("Failed!");  
		} 
		finally  
		{  
			reader.Close();  
		}
		





















			closeSqlConnection();	}	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	
	
	
	
	public static void OnApplicationQuit() {
		closeSqlConnection();
	}
	
	
	
	
	
	
	
	
	
	// Connect to database
	private static void openSqlConnection(string connectionString) {
		dbConnection = new MySqlConnection(connectionString);
		dbConnection.Open();
		result = dbConnection.ServerVersion;
		Debug.Log("Connected to database."+result);
	}
	
	// Disconnect from database
	private static void closeSqlConnection() {
		dbConnection.Close();
		dbConnection = null;
		//Debug.Log("Disconnected from database."+result);
	}
	// MySQL Query
	public static void doQuery(string sqlQuery) {
		IDbCommand dbCommand = dbConnection.CreateCommand();
		dbCommand.CommandText = sqlQuery;
		IDataReader reader = dbCommand.ExecuteReader();
		reader.Close();
		reader = null;
		dbCommand.Dispose();
		dbCommand = null;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}

Can you find my problemI checked many times but I cant finger out the problem
Thanks in advance

I’m not sure what you’re trying to do here exactly, but that code has (at least) a number of problems. For instance:

  • As I mentioned in your other thread, I can’t believe that you really want to open a db and load the contents of a table in OnGUI()…
  • Why do you have 2 back-to-back “while(reader.Read())” calls?
  • You’re trying to get content from reader1 without ever having done a reader1.Read()
  • Regarding your list of lists… Rather than trying to access your new list via its index, I’d probably tend to 1) create the new local list, 2) add the appropriate items to it, and then 3) add the new list to the containing list. That should be simpler and easier to read.

It’s unclear to me exactly what is in the tables you’re trying to load and how they relate to each other. However, I’m sure there’s a much easier way to accomplish whatever it is you’re trying to do.

Also, do yourself a favor and clean the code up. If you make a point to use proper indention, proper vertical spacing, and align braces/brackets/parens appropriately, it’ll make the code much easier to read and debug. Also, it’s always best to post the actual error message you’re getting - with the associated line/column information rather than paraphrasing it in your post.

Jeff

Thanks a lot!!
I tried to creat a list and add items and thhen add the lists to a list . That works! :smile:

Do you know why Unity is keeping some name even after changing the script?
For exemple : if I change the name of a label (first it was “it is good” , when i change into “it was good” , it is printing “it is good” but I change in the right script …

If it’s a public variable on a monobehaviour that’s placed on a gameobject somewhere, it will use what’s shown in the inspector.

Try this:

public String[,] listQuestions;