1

Complete noob here: What i'm trying to do is a simple registration screen where a text inputfields can be saved into an SQLite database. as you can see most of it are predefined strings as i am still testing it around. i have a script that should insert the values and it is attached to a button and calls the InsertScore function once it is clicked. the insert function works, it's just that i need to get the store the text from input field to a variable now or directly reference it here: cmd.Parameters.Add(new SqliteParameter("@EmployeeID",

Anyway, why can't i reference txtEmployeeID (the name of my Input Field) in C#? or better yet please show me how to reference the txtEmployeeID input field. that way i can get the string or the text inside it and save it into the database.

public void InsertScore()
{

connectionString = "URI=file:" + Application.dataPath + "/HighScoreDB.sqlite";

using (SqliteConnection con = new SqliteConnection(connectionString))
{



SqliteCommand cmd = new SqliteCommand();
cmd.CommandText = @"INSERT INTO HighScores(EmployeeID, EmployeeFirstname, TestScore, ROLE) VALUES (@EmployeeID,@EmployeeFirstName,@TestScore,@Role)";
cmd.Connection = con;
//cmd.Parameters.Add(new SqliteParameter("@EmployeeID", "222222"));
//cmd.Parameters.Add(new SqliteParameter("@EmployeeID", gameObject.GetComponent<input>
cmd.Parameters.Add(new SqliteParameter("@EmployeeFirstName", "Anand3"));
cmd.Parameters.Add(new SqliteParameter("@TestScore", "65"));
cmd.Parameters.Add(new SqliteParameter("@Role", "Tester"));

con.Open();

cmd.ExecuteNonQuery();

}

}


​
wh3resmycar2
  • 191
  • 1
  • 13

1 Answers1

1

how to reference the txtEmployeeID input field

Find the Input GameObject with GameObject.Find("txtEmployeeID") then perform GetComponent on the returned GameObject to get the InputField component.

private InputField employerIdinputField;

void Start()
{
    employerIdinputField = GameObject.Find("txtEmployeeID").GetComponent<InputField>();

    string dataFromInput = employerIdinputField.text;
}

To incorporate that to your current code:

private InputField employerIdinputField;

void Start()
{
    employerIdinputField = GameObject.Find("txtEmployeeID").GetComponent<InputField>();
}

public void InsertScore()
{

   connectionString = "URI=file:" + Application.dataPath + "/HighScoreDB.sqlite";

    using (SqliteConnection con = new SqliteConnection(connectionString))
    {

    SqliteCommand cmd = new SqliteCommand();
    cmd.CommandText = @"INSERT INTO HighScores(EmployeeID, EmployeeFirstname,    TestScore, ROLE) VALUES (@EmployeeID,@EmployeeFirstName,@TestScore,@Role)";
    cmd.Connection = con;

    //FIXED LINE!
    cmd.Parameters.Add(new  SqliteParameter("@EmployeeID",employerIdinputField.text));
    cmd.Parameters.Add(new SqliteParameter("@EmployeeFirstName", "Anand3"));
    cmd.Parameters.Add(new SqliteParameter("@TestScore", "65"));
    cmd.Parameters.Add(new SqliteParameter("@Role", "Tester"));
    con.Open();
    cmd.ExecuteNonQuery();
    }
}

For InputField event, check 3.InputField Component here.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • hey man thanks. it works. uhm follow up question please? i'm trying to reference the dropdown box but it insers some weird stuff in the SQLite database. how do i reference the values as text for a dropdown? – wh3resmycar2 Mar 19 '17 at 02:44
  • Hi, can you create a new question, include screenshot of what you want to access from the dropdown and I will provided. Right now, I don't exactly know what that is and I am trying to make each answer I provided answer the question in the title. Make sure to provide the current code you have for dropdown that doesn't work. – Programmer Mar 19 '17 at 02:49
  • i'm a little worried it will be tagged as a duplicate. anyway i will post a new question but to give a heads up: cmd.Parameters.Add(new SqliteParameter("@Role", employeeRole.value)); ... it returns the List Index Number instead of the Text value for some reason. – wh3resmycar2 Mar 19 '17 at 02:51
  • Make the title something like "Get value of Dropdown Menu". If it gets closed, I will modify that question and add my answer in it. I've been doing that for some people. – Programmer Mar 19 '17 at 02:53
  • hey man, i can't post another question after 90 minutes *sadface. – wh3resmycar2 Mar 19 '17 at 02:55
  • I really do think that this deserves it's own question. You use `dropdown.captionText.text` to get the selected text. You use `List dropdownOptions = dropdown.options;` to get all the options in the dropdown. You can the loop through them like `for (int i = 0; i < dropdownOptions.Count; i++) { Debug.Log(dropdownOptions[i].text); }` – Programmer Mar 19 '17 at 03:02