I am having a database name "fees" where i am having 4 columns (admno, receiptnumber, name, tutionfee). Here primary key is "receiptnumber". I have entered several records with different admission number. There is also several entries with same admission number. Now i just want to print only last record which i have entered. How can i print. I have written a code but first record is printing.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
string str;
str = "select * from fees where admno='" + temp + "'";
SqlCommand com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
reader.Read();
TextBox1.Text = reader["admno"].ToString();
TextBox2.Text = reader["receiptnumber"].ToString();
TextBox3.Text = reader["name"].ToString();
TextBox4.Text = reader["tutionfee"].ToString();
This should do it. Add parameters to avoid sql injection!
You need to order by your ID DESCending and pick the fist one (TOP1)
string Command = "select TOP 1 * from fees where admno = #admno ORDER BY receiptnumber DSEC";
using (SqlConnection mConnection = new SqlConnection(ConnectionString))
{
mConnection.Open();
using (SqlCommand cmd = new SqlCommand(Command, mConnection))
{
cmd.Parameters.Add(new MySqlParameter("#admno", temp));
using (SqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
TextBox1.Text = reader["admno"].ToString();
TextBox2.Text = reader["receiptnumber"].ToString();
TextBox3.Text = reader["name"].ToString();
TextBox4.Text = reader["tutionfee"].ToString();
}
}
}
Assuming receiptnumber is an identity field, you would select top 1 * from fees order by receiptnumber desc
Related
I want to fetch the maximum value of date from the table. But I always get an 'OutofRangeException' error. I changed my query several times.
Due_Date column has date data type
Due_Date_Sample is a string var
Due_Date_var is a DateTime var
My code:
using (SqlConnection sqlCon = new SqlConnection(Main.connectionString))
{
string commandString = "SELECT TOP 1 FORMAT(Due_Date,'dd-MM-yyyy') FROM Transactions WHERE Plot_Code='" + Plot_Code_var + "' ORDER BY Due_Date DESC;";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
sqlCon.Open();
SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
date_control_var = 2;
Due_Date_Sample = (dr["Due_Date"].ToString());
Due_Date_var = DateTime.Parse(Due_Date_Sample.ToString());
}
dr.Close();
}
You need to give an alias to the selected value, e.g.: FORMAT(Due_Date,'dd-MM-yyyy') as Due_Date
You can do this:
using (SqlConnection sqlCon = new SqlConnection(Main.connectionString))
{
string commandString = "SELECT TOP 1 FORMAT(Due_Date,'dd-MM-yyyy') as Due_Date FROM Transactions where Plot_Code='" + Plot_Code_var + "' ORDER BY Due_Date DESC;";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
sqlCon.Open();
SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
date_control_var = 2;
Due_Date_Sample = (dr["Due_Date"].ToString());
Due_Date_var = DateTime.Parse(Due_Date_Sample.ToString());
}
dr.Close();
}
How can I write this code in asp.net c# code behinds?
Wwhat I'm trying to do is to select all rows in invoicetable with orderno that is equal to current session and deduct the inventory of my inventorytable from `invoicetable qty that matches their itemid's.
SqlCommand cmd =
new SqlCommand("UPDATE inventorytable
JOIN invoicetable ON inventorytable.ItemID = invoicetable.ItemID
SET inventorytable.inventory = inventorytable.inventory-invoice.QTY
WHERE invoicetable.No='" + Convert.ToInt32(Session["invoiceno"]) + "'"
, con);
InsertUpdateData(cmd);
Your update query is not formed correctly, and you should be using parameterized SQL. Try using something like this
var sqlQuery =
#"UPDATE inventorytable
SET inventorytable.inventory = inventorytable.inventory-invoice.QTY
FROM inventorytable
INNER JOIN invoicetable ON inventorytable.ItemID = invoicetable.ItemID
WHERE invoicetable.No=#invNo";
using (var conn = new SqlConnection(CONN_STR))
{
var sqlCmd = new SqlCommand(sqlQuery, conn);
sqlCmd.Parameters.AddWithValue("#invNo", Session["invoiceno"].ToString());
sqlCmd.ExecuteNonQuery();
}
I typed this without VS in front of me, so let me know if there are any syntax issues
var n = Session["invoiceno"] != null ? Convert.ToInt32(Session["invoiceno"]) : 0;
using (var conn = new SqlConnection(CONN_STR))
{
conn.Open();
var sql = "SELECT * FROM invoicetable WHERE orderno = #n";
var cmd = new SqlCommand(sql);
cmd.Connection = conn ;
cmd.Parameters.AddWithValue("#n", n);
using(var dr = cmd.ExecuteReader())
{
while(dr.Read())
{
//loop through DataReader
}
dr.Close();
}
}
I have a datatable with 2 columns, ID and Name, I have populated my combobox with the column ID.
string Query = "SELECT * FROM [Database]";
OleDbConnection me = new OleDbConnection(connection);
OleDbCommand constr = new OleDbCommand(Query, me);
me.Open();
OleDbDataReader reader = constr.ExecuteReader();
while(reader.Read())
{
textBox15.Text = (reader["Name"].ToString());
}
reader.Close();
When I select an item from the combobox, I want to retrieve values from the Column Name in the same row. For instance I select a value from my combobox which is in datarow 1 and it matches the datarow 1 in the table Name
Is there anyway to do this?
I am currently here
{
string Query = "SELECT * FROM [Database] where Name ='" + comboBox6.Text + "' "; string y = textBox15.Text
OleDbConnection me = new OleDbConnection(connection);
OleDbCommand constr = new OleDbCommand(Query, me);
me.Open();
OleDbDataReader reader = constr.ExecuteReader();
constr.Parameters.Add(new OleDbParameter("#Name", y));
while (reader.Read())
{
textBox15.Text = reader["Name"].ToString();
}
me.Close();
}
}
I am still getting an error "No parameters given for one or more values" I am sure that the code is right.
You'll need to add a parameter to your SQL query. For example:
string myName = myComboBox.SelectedItem.Text;
string Query = "SELECT * FROM [Database] WHERE Name = ?";
OleDbConnection conn = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand(Query, conn);
cmd.Parameters.Add(new OleDbParameter("#name", myName));
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while(reader.Read())
etc...
I'm not sure of the exact syntax for the OLE DB .NET Provider, but hopefully this helps somewhat.
I failed to get the correct result with this code in Form2:
conn.Open();
OleDbCommand cmd = new OleDbCommand("Select * From udbTable Where Username Like '" + f1.textBox1.Text + "%'", conn);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
label5.Text = reader["Username"].ToString();
}
conn.Close();
I have 3 samples data in the table, but i'm always getting the same result which is the first entry of the database. Whenever i input the last entry or second entry in the textbox1.Text, i still getting the first entry.
textbox1.Text is from Form1, and i set it's property Modification to Public.
label5.text is the output.
try this fix
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection=conn;
command.CommandText = "Select * From udbTable Where Username Like ?";
cmd.Parameters.Add("#Username",OleDbType.VarChar);
cmd.Parameters["#Username"].Value=f1.textBox1.Text;
OleDbDataReader reader = cmd.ExecuteReader();
I am fetching records of users and binding it to gridview. I want to show the number of results returned like "15 results found". I used RecordsAffected but I think it wont work with select statement. Any alternate way?
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select name, city, number where age between " + from.Text + "AND " + to.Text;
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
reader.Close();
usersgrid.DataSource = cmd.ExecuteReader();
usersgrid.DataBind();
con.Close();
}
else
{
result.Visible = true;
}
}
}
Now I want to show number of rows returned on a label.
int affectedRows = cmd.ExecuteNonQuery();
that would work for you insted of cmd.ExecuteReader();
another alternative will be to get rowcount of the usersGrid control
int Count = usersGrid.Rows.Count-((usersGrid.PageCount-1) * usersGrid.PageSize);