Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am working for a asp.net website with oracle 11g database in backend. For a button click, i want to execute an update command. I have executed other commands elsewhere (insert,select,delete) but this command however is giving invalid identifier. The code is below:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Answer")
{
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
TextBox TextBox1 = row.FindControl("TextBox1") as TextBox;
string ID = row.Cells[0].Text;
string Date = row.Cells[2].Text;
string answer = TextBox1.Text;
string query = "update \"Review2\" set \"Answer\"='" + answer + "' where \"Cust_id\"=" + ID + " and \"Date\"=to_date('" + Date + "','yyyy-mm-dd hh24:mi:ss')";
SqlDataSource1.UpdateCommand = query;
SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
int result = SqlDataSource1.Update();
GridView1.DataBind();
}
}
when i execute this code, no update is being done and at line :int result = SqlDataSource1.Update(); , i get the error,{"ORA-00904: \"ASD\": invalid identifier\n"}. Any solution?
Your update query is wrong in syntax
try the below one
string query = #"update Review2 set Answer ='" + answer + "' where Cust_id =" + ID + " and Date =to_date('" + Date + "','yyyy-mm-dd hh24:mi:ss')";
Note:- Strongly recommend to use parameterized queries inorder to avoid SQL Injection
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to get "Model" field and put on Textbox6. But how come it does not work.
The problem is that the Model field answer will not be shown in the textbox6
string Query = "Select * from S where Name = '" + TextBox1.Text + "' and Clientno = '" + TextBox2.Text + "';";
command.CommandText = Query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string Model = reader.GetString(reader.GetOrdinal("Model"));
TextBox6.Text = Model;
}
Couple of things:
Do not use select *, instead use select your columns names
Do not pass the .Text directly to your query, instead use parameterized sql expression
If Clientno is primary key column or , combination of Name and ClientNo gives unique result, use ExecuteScalar, you don't have to use ExecuteReader and loop through the datareader
Since you using only one field and want to fill in the textbox, modify your select statement to :
select top 1 Model from S where....
And if you are reading only one row you will not need a while loop. Further, you should always close the reader and put your SqlConnection inside using block. ( edited as suggested by the comments)
If (reader.Read())
{
TextBox6.Text = reader.GetString(reader.GetOrdinal("Model"));
reader.Close();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
It is such that I have no date in the database, and if it is less than the time / date as we have now so it must go in and refresh the use of the will rank 2. This means that we have, for example. the time is now and if it is not right than he who is in the database so it should not go in and do anything at all.
That means if the date looks like this is it too old.
3/15/2015 09:34:42 AM (it is now.) and 3/15/2015 04:34:42 AM
So it must go into the code to update the user to get rank 2
while (readerBrugere.Read())
{
string brugerid = readerBrugere["id"].ToString();
DateTime brugerdato = Convert.ToDateTime(readerBrugere["trydato"].ToString());
conn1.Close();
if (Convert.ToDateTime(DateTime.Now.ToString("ddmmyyyyHHmmss")) > Convert.ToDateTime(brugerdato))
{
int rankid = 2;
cmd1.CommandText = "UPDATE brugere SET rank = #rank WHERE Id = #brugerid;";
cmd1.Parameters.AddWithValue("#rank", rankid);
cmd1.Parameters.AddWithValue("#brugerid", brugerid);
conn1.Open();
cmd1.ExecuteNonQuery();
conn1.Close();
}
}
It will only run content through if it is to time with me is right than what is in the database
The text of your question hard to decipher, but looking at the code I can suggest a much-improved version:
while (readerBrugere.Read())
{
//if these columns cannot be null, you should be able to use a simple cast
int brugerid = (int)readerBrugere["id"];
DateTime brugerdato = (DateTime)readerBrugere["trydato"];
//don't close your database connection here
//These values are *already* datetimes. There's not need to convert to datetime
// and *especially* no need to convert to strings to compare them
if (DateTime.Now > brugerdato)
{
// the "2" is a constant: just code it into the query that way
string sql = "UPDATE brugere SET rank = 2 WHERE Id = #brugerid;";
//or maybe you wanted this instead:
string sql = "UPDATE brugere SET rank = rank + 1 WHERE Id = #brugerid;";
//AddWithValue() kinda sucks somtimes. I prefer to do it like this instead:
cmd1.Parameters.Add("#brugerid", SqlDbType.Int).Value = brugerid;
//don't use the same connection as you did for the outer query:
using (var cn2 = new SqlConnection(conn1.ConnectionString))
using (var cmd2 = new SqlCommand(sql, cn2))
{
cmd2.ExecuteNonQuery();
}
}
}
Finally, all this is really moot, as I expect you can do this entire process in the database with a single query:
UPDATE brugere
SET rank = rank + 1
WHERE trydato <= current_timestamp AND brugerdi IN
(
...existing query goes here, but only select the brugerdi column ...
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code
public static DataSet tipopromocion(string id_edificio,string date_from, string date_to)
{
DataSet tipo = new DataSet();
MySqlConnection con = new MySqlConnection(connectionstring);
string tipo_promo = "select pr.tipo_promocion from promocion_edificio pe inner join inventario i on i.id_edificio = pe.id_edificio inner join promocion pr on pe.id_promocion=pr.id_promocion where i.id_edificio = '" + id_edificio + "' and i.fecha between '" + date_from + "' and Date_Sub('" + date_to + "',interval 1 Day) and i.fecha between pe.inicio_promo AND pe.fin_promo and date(now()) between pe.inicio_alojamiento and pe.fin_alojamiento AND ( FIND_IN_SET('A',pe.tipo)) group by pe.id_promocion order by pr.valor_promocion desc";
MySqlCommand cmd13 = new MySqlCommand(tipo_promo, con);
MySqlDataAdapter da13 = new MySqlDataAdapter(cmd13);
da13.Fill(tipo);
return tipo;
}
tipo_promonic
-------------
porcentaje
porcentaje
fixed
discount
porcentaje
discount
discount
fixed
fixed
porcentaje
porcentaje
the above result like above table table ok now i am trying to check in "tipo_promonic" column contains some rows so i am checking if "tipo_promonic" column contains porcentaje,fixed,discount then go to some function else it goto another function ok but in "tipo_promonic" column contains many duplicate values so how to check the condition. please help me out from this problem.
Try Re-framing your SQL query using DISTINCT keyword to avoid Duplicate record
string tipo_promo = "select DISTINCT pr.tipo_promocion from promocion_edificio pe inner ..."
Try using DataReader
MySqlConnection con = new MySqlConnection(connectionstring);
string tipo_promo = "select DISTINCT pr.tipo_promocion from promocion_edificio pe inner.."
MySqlCommand cmd13 = new MySqlCommand(tipo_promo, con);
DataReader dr = cmd13.ExecuteReader();
while(dr.Read())
{
if(dr[0].ToString() == "YourOption")
{//Do this;}
}
This is for a data table. You can make out for DataAdapter or Dataset according to your need. This is basically a loop which checks each row for a value of a particular column.
foreach (DataRow dr in dataTable.Rows)
{
if (dr["typo_promonic"].Equals("porcentaje"))
{
//your code here.
}
else if (dr["tipo_promonic"].Equals("discount"))
{
//your code here.
}
else
{
//your code here.
}
}
You can add more else...if as per your options. Whatever you want to do, you can do in those code blocks.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In this query statement i want sum of the number of days in the 'datetime' column where emp_ID equals to selected emp_ID in the textBox2 'and' leavetype = Fullday 'and' status = approved. this is my code
string selectSql =
"Select sum(datetime)
From Lea_information
Where emp_ID= ('" + textBox2.Text + "')
and (leave_type,status) = values (Fullday,Approved)";
Try like this
string selectSql =
"Select sum(datetime)
From Lea_information
Where emp_ID= '" + textBox2.Text + "'
AND leave_type ='Fullday'
AND status = 'Approved'";
Use
ing selectSql = "Select sum(datetime) from Lea_information where emp_ID= ('" + textBox2.Text + "') and leave_type = FullDay and status = Approved)";
Not sure which database you are using, but I've never seen statements like you have written there before.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to select id from one table and insert that id to another table.
First I need to read my first table id and want to pass it to string.
Then I need to pass that string to second table.
String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + TextBox1.Text + "';";
{
int MId = int.TryParse(Id);
String QueryStr = "INSERT INTO User_Images VALUES (#Image)";
SqlCommand scmd = new SqlCommand(QueryStr, conn);
SqlDataReader sqldread = scmd.ExecuteReader();
//String QueryStr = "UPDATE MapDataImage SET Image = #Image WHERE Source='" + TextBox1.Text + "';";
//SqlCommand scmd = new SqlCommand(QueryStr, conn);
scmd.Parameters.Add("#Image", SqlDbType.VarBinary).Value = imgbytes;
scmd.ExecuteNonQuery();
}
so is this correct?
int MId = int.TryParse(Id); //the name id does not exist in current context?
but i want to retrieve particular id value from database
or
int MId = int.TryParse(#Id);
int MId = int.Parse("Id");
Will never work. "Id" is a string literal, it can never be an integer. I think you need to specify a variable
int mId = int.Parse(id);
Aside that, try using TryParse so it's safer.
Also use paramerterised queries on your SELECT statement to prevent SQL Injection.
Please post the rest of your code and I will adjust my answer to accomodate.
You don't create DataReaders objects by yourself. Instead you obtain a reference to DataReader object by invoking ExecuteReader method of Command class.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader.aspx