I am working on a project where the user is displayed a image with hotspots. Upon clicking one part he is displayed a dynamically generated checkbox for which the values are picked from database (hotspot are mapped to value displayed).
The problem I am facing is that when the value is a single word (ex. swelling) the code works fine and fetches the possible diseases, but when there are words like (ex. joint pain or nausea with vomiting) i.e the ones which contain space between them (more than one word as a checkbox value) the code does not work.
Here is the code
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox2.Text != "")
{
connection.Open();
symptons = String.Join(", ", CheckBoxList1.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Text).ToArray());
Label1.Text = symptons;
string query = symptons.Replace(", ", "','");
string cm = "select distinct dname from disease d inner join diseasesymptom ds on ds.did=d.did inner join symptom s on s.sid=ds.sid where s.sname in ('" + query + "')" + "and days >" + TextBox2.Text + " and days<41 order by (days) desc;";
if (symptons != "")
{
MySqlCommand cmd = new MySqlCommand(cm, connection);
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = connection;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
else
{
Label1.Text = "select at least one symptom";
}
}
else
{
string script = "alert(\"We can't predict without all inputs :(\");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
}
}
I think it has something to do with Join and Replace that I am performing.
never mind guys..I got it. I was missing a column value in database that helped in searching (hint : its 'days') . Anyway thanks for anyone who read this. :)
Related
I'm trying to somehow update or retrieve again data from Access database (Random piece of data with column names)
I'm using Microsoft.ACE.OLEDB.12.0.
I created a query which should return records where a Column has a value between two dates:
SQL example:
SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia, Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki
FROM Arkusz1
WHERE(
((Arkusz1.Umówione) = Yes) // or 1 or <> 0
AND ((Arkusz1.Zakończone) = No) // or 0 doesen't provide right answer
AND ((Arkusz1.Data_umówienia) BETWEEN 24/07/2021 AND 26/07/2021) )
ORDER BY Arkusz1.Data_umówienia;
My code:
void fillGrid()
{
string teraz = DateTime.Today.AddDays(-1).ToShortDateString().Replace('.', '/');
string jutro = DateTime.Today.AddDays(1).ToShortDateString().Replace('.', '/');
// AddDays +-1 because of my data issue, that's not important
string dzisiejsze = "SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia,"
+ " Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki FROM Arkusz1"
+ " WHERE( ((Arkusz1.Umówione) = Yes) AND ((Arkusz1.Zakończone) = No) AND "
+ "((Arkusz1.Data_umówienia) BETWEEN "
+ teraz + " AND " + jutro + ") )"
+ " ORDER BY Arkusz1.Data_umówienia;";
//selecting almost every column from my only sheet (Arkusz1),
//if Umówione (appointment is arranged) has True value AND issue is not ended (Zakończone)
//AND appointment date is between yesterday and tomorrow
//(as I said, that's all because my stupid ideas in creating DB)
connection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dzisiejsze, connection);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
TodaysPlan_dataGridView.DataSource = dt;
TodaysPlan_dataGridView.Refresh();
connection.Close();
}
I even added a special button to update the whole grid (firstly it was supposed to update after a second Form is closed):
private void refreshGrid_Click(object sender, EventArgs e)
{
Application.DoEvents(); // because I saw something on stack, didn't work
fillGrid(); //start filling again, didn't work too
TodaysPlan_dataGridView.Refresh(); // same, didn't work
}
I don't get any error, execution (even in debug-mode) just passes by the code in refreshGrid_Click.
Edit: Tried to execute that SQL code for random data, didn't work too
Ofc that's not the most important part of my question but I could ask that in addition :)
I'll also provide code for saving button from second form, but in my opinion it won't be really helpful:
private void save_button_Click(object sender, EventArgs e)
{
if ((Tick_label.Visible == true) && (IDInfo_label.Text == "All good!") && (street_textBox.TextLength > 0))
{
connection.Open();
string cmd_string = "INSERT INTO Arkusz1 (ID, Adres, Przydzielenie) VALUES ('" + ID_maskedTextBox.Text + "', '" + street_textBox.Text + "','" + startDate_dateTimePicker.Value + "')";
OleDbCommand command = new OleDbCommand(cmd_string, connection);
command.ExecuteNonQuery();
connection.Close();
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("error");
}
}
That's my first question on Stack, any suggestions and constructive criticism are welcome!
BTW, I was using this YT tutorial, hope it helps someone :)
All right, informations provided by #Steve and #Jimi were really helpful, firstly I changed string formatting using String.Format() and resolved SQL problem - which was (#date#) formatting:
DateTime tmp_teraz = DateTime.Today.AddDays(-1);
string teraz = String.Format("{0}/{1}/{2}", tmp_teraz.Month.ToString(),
tmp_teraz.Day.ToString(), tmp_teraz.Year.ToString());
DateTime tmp_jutro = DateTime.Today.AddDays(1);
string jutro = String.Format("{0}/{1}/{2}", tmp_jutro.Month.ToString(), tmp_jutro.Day.ToString(), tmp_jutro.Year.ToString());
string dzisiejsze = String.Format("SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia, Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki FROM Arkusz1 WHERE( ((Arkusz1.Umówione) = true) AND ((Arkusz1.Zakończone) = false) AND ((Arkusz1.Data_umówienia) " +
"BETWEEN (#{0}#) AND (#{1}#)) ) ORDER BY Arkusz1.Data_umówienia;", teraz, jutro);
At the end, grid looks like this.
Refreshing or inserting data with dataGrid doesen't work, but that's a thing I'll develop over time.
Thanks for help and have a nice day guys!
Is there anything wrong with my code? It is not showing data in textboxes. The same funtion is working for another table in database but not for this one.
private void metroButton1_Click(object sender, EventArgs e)
{
con = new SqlConnection(constr);
String query = "Select FROM Student WHERE Std_ID = '" + metroTextBox1.Text + "'";
cmd = new SqlCommand(query, con);
con.Open();
try
{
using (SqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
// metroTextBox1.Text = (read["ID"].ToString());
metroTextBox2.Text = (read["Name"].ToString());
metroTextBox3.Text = (read["F_Name"].ToString());
metroTextBox4.Text = (read["Std_Age"].ToString());
metroTextBox5.Text = (read["Address"].ToString());
metroTextBox6.Text = (read["Program"].ToString());
metroComboBox1.Text = (read["Course"].ToString());
}
}
}
finally
{
con.Close();
}
}
you need to give column names in the select statement or select *
for example :
String query = "Select * from Student WHERE Std_ID = '" + metroTextBox1.Text + "'";
Not related to Question: you can change the while loop to if condition if you have one record for given id. even there are many records for given id you will see the last record data only because of the while loop will overwrite the textboxes in every record.
Update :
There isn't anything wrong with Syntax because the same syntax is
working for modifying teacher funtion.
No, this is incorrect, remove the try catch in your code then you will see the exception of syntax error
I have a particular part of an inventory interface that requires an employee to select his or her name from a combo box and then scan a product to the table assigned to the name of the employee.
My curiosity is: When hitting the EDIT, ADD OR DELETE button it knows what table to perform this function in from a Switch - Case statement with that employee name on it. The problem is, the piece of code is long for each employee, especially for 9 employees that each have a Switch - Case statement.
Any advice on how to simplify this or shorten the code? I do understand in advance about the parameterized SQL that I am failing to use. Just trying to accomplish this first.
private void btnAdd_Click(object sender, EventArgs e)
{
ActiveControl = txtSerialN;
if (!string.IsNullOrEmpty(txtSerialN.Text) && !string.IsNullOrEmpty(cboEmpName.Text))
switch (cboEmpName.SelectedItem.ToString().Trim())
{
case "John Doe":
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "INSERT INTO JohnDoe(SerialNumber,PartNumber,DateEntered,Customer) values ('" + txtSerialN.Text + "','" + txtPart.Text + "','" + txtDate.Text + "','" + txtCustomer.Text + "')";
command.ExecuteNonQuery();
MessageBox.Show("Inventory Added".PadLeft(23));
connection.Close();
txtSerialN.Clear();
txtPart.Clear();
txtDate.Clear();
txtCustomer.Clear();
command.CommandText = "SELECT * FROM JohnDoe ORDER BY PartNumber";
OleDbDataAdapter db = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
db.Fill(dt);
dataGridEmpParts.DataSource = dt;
}
catch (OleDbException)
{
string strmsg = "THIS SERIAL NUMBER ALREADY EXISTS ! , Please try again";
MessageBox.Show(strmsg, "YOU CAN'T ENTER THE SAME ONE AGAIN", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
connection.Close();
}
break;
}
}
I would rather put up a lookup table that will have columns such as EmployeeName, AssignedTable and dynamically construct the commandtext based on the parameter values.
I suspect this problem could more efficiently be fixed by altering the database. Perhaps even as simple as adding a field for employee name.
This program uses fileds there is no data grid. what I am struggling with or you can say I want to correct functions to this is that.
If a user enters ID "1234" and this data already exists in the SQL Database instead of the program crashing or through exception, shows a pop up alert that the data already exists.
The filed Year is a combo box instead of a text box, where we can see years like in a drop down list and if user chooses ID "1234" and chooses 2016 for this only the corresponding amount should appear in the amount text box, or if there are other years for this user ID can be selected and checked the amount.
The program is listing but not showing the year properly, my code is as follows :-
private void btnSearch_Click(object sender, EventArgs e)
{
cmbYear.Items.Clear();
string sql = "";
con.Open();
SqlCommand cmd = new SqlCommand();
try
{
sql += "SELECT m.MemberId, m.Name, m.Address, m.Cellular, m.Email, p.PaymentId, p.Year, p.Amount from Members as m";
sql += " INNER JOIN Payments as p ON m.MemberId = p.MemberId";
sql += " WHERE m.MemberId = '" + tbID.Text + "' ORDER BY p.Year ASC";
cmd.Connection = con;
cmd.CommandText = sql;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
if(dt.Rows.Count >0)
{
for(int i = 0; i<=dt.Rows.Count -1;i++)
{
tbID.Text = dt.Rows[i]["MemberId"].ToString();
tbName.Text = dt.Rows[i]["Name"].ToString();
tbCellular.Text = dt.Rows[i]["Cellular"].ToString();
tbEmail.Text = dt.Rows[i]["Email"].ToString();
tbAddress.Text = dt.Rows[i]["Address"].ToString();
tbAmount.Text = dt.Rows[i]["Amount"].ToString();
cmbYear.Items.Add(dt.Rows[i]["Year"].ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
//This part displaying og the existing data from all the fileds
I'm trying to update my database records, but no changes are made and no error messages. I checked the syntax, the values I'm sending, everything is just fine ..
any suggestions?
This is my code which executed when [save] button is clicked:
ds.UpdateCommand = "UPDATE Users
SET Fullname='" + fname.Text + "',
Permission='" + per.SelectedValue + "',
Email='" + email.Text + "',
phone='" + phone.Text + "'
WHERE UserID=" + Session["userID"].ToString();
ds.Update();
I'm reading values from form filled by the user
ds is an SqlDataSource
If I have to add more details let me know
EDITS:
This page is for user to update his/her information
I'm setting the form values on Page_Load depending on the users information already exist in database.
the user edits his/her info and click [Save]
after setting braekpoints, I found that query string is taking the default values not the new ones. what should I do?
The entire code:
protected void Page_Load(object sender, EventArgs e)
{
Session["userID"] = Request.QueryString["id"];
SqlConnection cn = new SqlConnection();
cn.ConnectionString = ds.ConnectionString;
cn.Open();
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "select * from Users where UserID='" + Session["userID"].ToString() + "'";
SqlDataReader dr;
dr = cm.ExecuteReader();
if (dr.Read())
{
uname.Text = dr["username"].ToString();
fname.Text = dr["Fullname"].ToString();
per.SelectedValue = dr["Permission"].ToString();
email.Text = dr["Email"].ToString();
phone.Text = dr["phone"].ToString();
}
else Response.Redirect("Default.aspx");
dr.Close();
cn.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
ds.UpdateCommand = "update Users set Fullname='" + fname.Text + "', Permission='" + per.SelectedValue + "', Email='" + email.Text + "', phone='" + phone.Text + "' where UserID=" + Session["userID"].ToString();
ds.Update();
Response.Redirect("control_pan.aspx");
}
Basically, if you have a DataSet and you want to use that to update your database, you need to:
define the UpdateCommand as shown in the MSDN documentation to reference the columns from the DataTable which will be used to update
update an existing row in one of your DataTables inside the DataSet
once you've done that, then you can call .Update() on the data set (or data table) to execute the update - ADO.NET will check for updates to any of the rows of the DataTable, and if an update is found, then the UpdateCommand will be executed, with the parameters bound to the values of the DataTable's row in question
I would also recommend to read up on how the ADO.NET data model and using DataSets and DataTables works in detail - e.g. here Update Data Using .NET DataSets
The alternative, of course, would be to create a SqlConnection and a SqlCommand, using a parametrized query to do the insert yourself, without all the hassle and effort involved with DataSets and DataTables. But in that case, make sure to ALWAYS use parameterized queries (and NEVER just concatenate together your SQL statement including values straight from user input .....) - see why here
I suspect the Session["UserID"] is null. To check this set break point on ds.Update(); by putting the cursor on it then pressing F9.
To see the result query hover your mouse pointer over ds.UpdateCommand when break point pauses operation.
Update: put the code in the page load to be executed only once that is when first the page loads
if(!IsPostBack)
{
//put your code here
}
Update
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Session["userID"] = Request.QueryString["id"];
SqlConnection cn = new SqlConnection();
cn.ConnectionString = ds.ConnectionString;
cn.Open();
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "select * from Users where UserID='" + Session["userID"].ToString() + "'";
SqlDataReader dr;
dr = cm.ExecuteReader();
if (dr.Read())
{
uname.Text = dr["username"].ToString();
fname.Text = dr["Fullname"].ToString();
per.SelectedValue = dr["Permission"].ToString();
email.Text = dr["Email"].ToString();
phone.Text = dr["phone"].ToString();
}
else Response.Redirect("Default.aspx");
dr.Close();
cn.Close();
}
}
I seriously doubt you've provided enough details here to resolve the issue.
That type is UserID? Does the value need to be enclosed in quotes?
Are you setting the right value in your WHERE clause, and does that value existing in the database? You need to look at the resulting query string and then run it manually to determine what might be wrong.
Also, shouldn't you have the # character prefix for your string so that newlines are part of your string? Is this really what your code looks like?
Of course, without knowing more about the code, it's hard to say what else it might be as well.