Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi everyone
i have an database with a column name Lot Number
and according to production there will be multiple Items under the Lot number like weight and date.
so here i want to sum the data (values) of the same Lot number;
so what will be the logic or code.
i have done the selecting part like
"select (weight)from (database) where lot_number='"+textbox1.text+"';
now how can i sum the values under the same lot number.
suppose for lot number 1001
weights are ;100,1000,10000,2000,20000 ..........2000 etc
Posted
Updated 7-Jan-14 1:46am
v2

Hi Shantanu,

Its so simple you can try this, simple and sweet ;-)
SQL
sqlCmd = "select SUM(weight) from ([database].tablename) where lot_number='"+textbox1.text+"';
      SqlCommand cmd = new SqlCommand(sqlCmd, conn);
 
       conn.Open();
 
       object resultResult = cmd.ExecuteScaler();
textbox2.text=totalResult.ToString();

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v3
Comments
Shantanu sinha 7-Jan-14 7:51am    
and how can i display it in the textbox with TextBox1 (sum)
♥…ЯҠ…♥ 7-Jan-14 7:58am    
what is that? could you brief it?
Shantanu sinha 7-Jan-14 8:00am    
i have to display this sum into textbox2 so how can i do that
txs a lot
♥…ЯҠ…♥ 7-Jan-14 8:02am    
updated my answer, try and lemme know
You can do, for instance:
(produces a row with total weight for given lot number)
SQL
select sum(weight) as tot_weight from database where lot_number=..


or

(produces many rows, reporting the total weight of all the lot numbers)
SQL
select sum(weight) as tot_weight from database group by lot_number
 
Share this answer
 
v2
Comments
Shantanu sinha 7-Jan-14 7:49am    
and how can i display it in the textbox with TextBox1 (sum)
CPallini 7-Jan-14 8:05am    
See, for instance
http://forums.asp.net/t/1690372.aspx
You should refer sql CRUD operation.

This is the simple query.
SQL
Select sum(weights) from table where lot_number='"+textbox1.text+"'


Hi for that purpose you can write your code in datalogic as following

C#
SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["connSpionshopString"].ConnectionString;
  
string sqlCmd = "Select sum(weights) as TotalWeight from table where lot_number='"+textbox1.text+"';";
       SqlCommand cmd = new SqlCommand(sqlCmd, conn);

       conn.Open();

       object value = cmd.ExecuteScaler();
        
textbox1.text=value.ToString();
       conn.Close();
 
Share this answer
 
v3
Comments
Shantanu sinha 7-Jan-14 7:47am    
txs for ur reply. i want to sum the values.kindly see the question once again
txs for ur reply and kindly help
Shantanu sinha 7-Jan-14 7:51am    
and how can i display it in the textbox with TextBox1 (sum)
BK 4 code 7-Jan-14 7:57am    
Please see my Updated solutions
Shantanu sinha 7-Jan-14 8:07am    
SqlConnection con = new SqlConnection("Data Source=SHANTANU\\SQLEXPRESS;Initial Catalog=ankur;Integrated Security=True");
// con.Open();
string query = "select sum(Net_Wt) from Packing where Lot_No='" + TextBox1.Text + "'";

SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Open();
object value = cmd.ExecuteScaler();// here i m getting the error

TextBox2.Text = value.ToString();
con.Close();
Response.Redirect("Outwards.aspx");
}
Shantanu sinha 7-Jan-14 10:34am    
my weight is in varchar and it is not taking sum any solution for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900