1

I have many fields I'm trying to retrieve the data from to pass to the MySQL database.

This is the method I am using right now:

    ...
    PlaceHolder MAIN = Page.FindControl("MAIN_PH") as PlaceHolder;
    HtmlTable tblMain = MAIN.FindControl("Table1") as HtmlTable;
    HtmlTableRow ROW1 = tblMain.FindControl("ROW1") as HtmlTableRow;
    HtmlTableCell R1_CELL = ROW1.FindControl("R1_CELL") as HtmlTableCell;
    TextBox Field1 = R1_CELL.FindControl("Field1") as TextBox;
    HtmlTableRow ROW2 = tblMain.FindControl("ROW2") as HtmlTableRow;
    HtmlTableCell R2_CELL1 = ROW2.FindControl("R2_CELL1") as HtmlTableCell;
    DropDownList Field2= R2_CELL1.FindControl("Field2") as DropDownList;
    HtmlTableCell R2_CELL2 = ROW2.FindControl("R2_CELL2") as HtmlTableCell;
    DropDownList Field3= R2_CELL2.FindControl("Field3") as DropDownList;
    ...

    ...
    MySqlConnection conn = new MySqlConnection();
    conn.ConnectionString = getConnectionString().ToString();
    conn.Open();
    MySqlCommand cmd = new MySqlCommand();
    cmd.Connection = conn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "UPDATE_RECORD";
    cmd.Parameters.Clear();
    cmd.Parameters.Add(new MySqlParameter("@FIELD1", Field1.Text)); 
    cmd.Parameters["@FIELD1"].Direction = ParameterDirection.Input;
    cmd.Parameters.Add(new MySqlParameter("@FIELD2", Field2.SelectedValue.ToString())); 
    cmd.Parameters["@FIELD2"].Direction = ParameterDirection.Input;
    cmd.Parameters.Add(new MySqlParameter("@FIELD3", Field3.SelectedValue.ToString()));  
    cmd.Parameters["@FIELD3"].Direction = ParameterDirection.Input;
    ...

I've looked at the following:

https://stackoverflow.com/a/4955836/11035837

C#, FindControl

https://stackoverflow.com/a/1457615/11035837

but these seem to be good for if I was to do a distinct call to MySQL per Field value. (Especially if I stored some of the connection string data into the controls themselves as arguments).

Is there a better way to fetch all these values?

Something like:

    Field1= SomeFunction(ControlName).Text
    Field2= SomeFunction(ControlName).SelectedValue

without having to write the same code above as its own method?

I could certainly write a separate method that has the tedious check for each control tier/branch, and it might still save me some time in the end, but is there a way that can save me time both now and later?

wolfsshield
  • 757
  • 5
  • 14

0 Answers0