using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace flyff_acp
{
public partial class Info : Form
{
private ArrayList sql_data;
private ArrayList sql_columns;
private string table_name;
private List<Label> labels = new List<Label>();
private List<TextBox> textboxes = new List<TextBox>();
private int left_constant_lbl = 13;
private int center_constant_lbl = 425;
private int right_constant_lbl = 219;
private int interval = 48;
private int left_constant_txt = 16;
private int center_constant_txt = 428;
private int right_constant_txt = 222;
private string current_side = "left";
private int new_y_lbl = 15;
private int new_y_txt = 31;
private int current_id = -1;
private string update;
private string where;
public Info(string tablename, ArrayList data, ArrayList columns)
{
InitializeComponent();
table_name = tablename;
sql_data = data;
sql_columns = columns;
if (sql_data != null)
{
showAs.Items.AddRange(sql_columns.ToArray());
showAs.SelectedIndex = 0;
reloadListBox(0);
}
else
{
no_data_lbl.Visible = true;
}
}
private void reloadListBox(int index)
{
data_values.Items.Clear();
foreach (object[] item in sql_data)
{
data_values.Items.Add(item[index]);
}
}
private void showAs_SelectedIndexChanged(object sender, EventArgs e)
{
reloadListBox(showAs.SelectedIndex);
}
private void data_values_SelectedIndexChanged(object sender, EventArgs e)
{
if (current_id != data_values.SelectedIndex)
{
foreach (Label lbl in labels)
{
this.Controls.Remove(lbl);
}
foreach (TextBox txtbox in textboxes)
{
this.Controls.Remove(txtbox);
}
current_id = data_values.SelectedIndex;
current_side = "left";
new_y_lbl = 15;
new_y_txt = 31;
reloadData();
for (int i = 0; i < textboxes.Count; i++)
{
TextBox txt = textboxes[i];
Label lbl = labels[i];
if (i == textboxes.Count - 1)
{
where += lbl.Text + "='" + txt.Text + "'";
}
else
{
where += lbl.Text + "='" + txt.Text + "' AND ";
}
}
}
}
private void reloadData()
{
for (int i = 0; i < sql_columns.Count; i++)
{
string current_column = sql_columns[i].ToString();
Label temp = new Label();
TextBox temp_txt = new TextBox();
if (current_side == "left")
{
temp.Location = new System.Drawing.Point(left_constant_lbl, new_y_lbl);
temp_txt.Location = new System.Drawing.Point(left_constant_txt, new_y_txt);
current_side = "center";
}
else
if (current_side == "center")
{
temp.Location = new System.Drawing.Point(center_constant_lbl, new_y_lbl);
temp_txt.Location = new System.Drawing.Point(center_constant_txt, new_y_txt);
current_side = "right";
}
else
{
temp.Location = new System.Drawing.Point(right_constant_lbl, new_y_lbl);
temp_txt.Location = new System.Drawing.Point(right_constant_txt, new_y_txt);
new_y_lbl = new_y_lbl + interval;
new_y_txt = new_y_txt + interval;
current_side = "left";
}
temp.Name = current_column + "_lbl";
temp.Text = current_column;
temp.Visible = true;
temp.Size = new System.Drawing.Size(temp.PreferredWidth, temp.PreferredHeight);
temp_txt.Name = current_column + "_txt";
temp_txt.Text = (string)((object[])sql_data[current_id])[i].ToString();
temp_txt.Visible = true;
temp_txt.Size = new System.Drawing.Size(171, 20);
labels.Add(temp);
textboxes.Add(temp_txt);
this.Controls.Add(temp);
this.Controls.Add(temp_txt);
}
}
private void Update_Btn_Click(object sender, EventArgs e)
{
for (int i = 0; i < textboxes.Count; i++)
{
TextBox txt = textboxes[i];
Label lbl = labels[i];
if (i == textboxes.Count - 1)
{
update += lbl.Text + "='" + txt.Text + "'";
}
else
{
update += lbl.Text + "='" + txt.Text + "', ";
}
}
Log Logs = new Log();
string query = "UPDATE " + table_name + " SET " + update + " WHERE " + where;
string con_string = Logs.ReadConfigs();
SqlConnection con = new SqlConnection(con_string);
try
{
con.Open();
}
catch
{
Logs.ErrorLog("Unable to create a connection to the SQL server on the Information form.");
throw new Exception("Unable to connect to the SQL server.");
}
SqlCommand cmd = new SqlCommand(query, con);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Data updated!");
this.Close();
}
catch
{
Logs.ErrorLog("Unable to execute a query on the Information form. Query: " + query);
throw new Exception("Unable to execute query.");
}
}
}
}