1

What is wrong with this code? I want to catch the last value from SQL row and display it in a TextBox. Kindly help me.

private void textBox2_Leave(object sender, EventArgs e)
{
    cmd.Connection = cn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select last(remain) from item_new_customer where cust=" + textBox2.Text + "";
    float h = (float)cmd.ExecuteScalar();
    textBox20.Text = h.ToString();
}
7
  • What is the error you are receiving? Commented Jan 28, 2013 at 12:39
  • 1
    Your question doesn't really make sense. The title says last but your code sample says max. What issues are you seeing? Commented Jan 28, 2013 at 12:39
  • Maybe you need to enclose it in single quotes like ' for textbox value, also look into parameterized queries Commented Jan 28, 2013 at 12:39
  • Please change your title.. Commented Jan 28, 2013 at 12:39
  • 2
    DO NOT send data directly from the UI to the database. You are leaving yourself wide open for SQL Injection Commented Jan 28, 2013 at 12:43

5 Answers 5

2
cmd.CommandText = "select max(remain) from item_new_customer where cust='" + textBox2.Text + "'";
Sign up to request clarification or add additional context in comments.

1 Comment

What if the user enters 'user42; drop table item_new_customer; go; in the text box?
1

You are open for SQL-Injection, use parameters to avoid it.

To answer your actual question, i assume that you want this column: remain. But you want the value of the last inserted record. Since you haven't mentioned the column to detect the order of insertion, i use the primary key column (not recommended):

string sql = "SELECT TOP 1 remain FROM dbo.tablename WHERE cust=@cust ORDER BY id DESC";
using(var con = new SqlConnection(connectionString))
using(var cmd = new SqlCommand(sql, con))
{
    cmd.Parameters.AddWithValue("@cust", textBox2.Text);
    con.Open();
    double h = (double)cmd.ExecuteScalar();
    textBox20.Text = h.ToString();
}

7 Comments

Why row_number instead of TOP 1 ... ORDER BY id DESC?
@ta.speot.is: There are more than one possible ways, you could also use a sub-query(WHERE id=(SELECT MAX(Id)...)
And row_number does not really express intent that well. I think you will find the execution plan is more expensive, too.
@ta.speot.is: Point taken :)
I have a new problem in this (float h = (float)cmd.ExecuteScalar();)
|
0

You're missing a single quote after the textBox2.Text:

private void textBox2_Leave(object sender, EventArgs e)
{
    cmd.Connection = cn;
    cmd.CommandType = CommandType.Text;


    cmd.CommandText = 
   "select max(remain) from item_new_customer where cust=" + textBox2.Text + "'";
                                                          //Missing tick here ^ 
    float h = (float)cmd.ExecuteScalar();
    textBox20.Text = h.ToString();
}

In addition, your code is an open invitation for SQL injection.

4 Comments

SQL Injection is not limited to web applications.
this project is not a web project.
@ahmed, as ta.speot.is pointed out, it's not just web projects that are vulnerable to SQL injection attacks.
My problem in (float h = (float)cmd.ExecuteScalar();)
0

Thanks very much for all My final right code is:

cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select top 1(remain) from item_new_customer where cust='"+textBox2.Text+"' order by id desc";
        int h = (int)cmd.ExecuteScalar();
        textBox20.Text = h.ToString(); 

1 Comment

if the name typed into textBox2.Text is not exist in SQL table the program how can tell me this name is not exist in table?
0

What I would just do is put all of the SQL row values into a listbox and then take the text of the last item in the textbox and put that into a textbox. Keep the listbox hidden

    private System.Windows.Forms.ListBox listBox1;
    static SqlConnection connection = new SqlConnection(@"Data Source=hostname;Initial Catalog=database_name;Integrated Security=False;User ID=user;Password=123456;");
    SqlDataAdapter adapter = new SqlDataAdapter("select * from table_name", connection);
    DataTable table = new DataTable();
    adapter.Fill(table);
    foreach (DataRow row in table.Rows)
    {
    listBox1.Items.Add(row["row_name"].ToString());
    }
    textBox20.Text = listBox1.Items[listBox1.Items.Count - 1].ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.