<div>i have two tables account_mstr and trans_mstr and i want to insert the subtraction of two columns curbal - amt of two different tables and want to store the result in another column balance of table trans_mstr .</div> <div>i found the solution of sql server which is in link mentioned below:</div> <div></div> <div>http://forums.asp.net/t/1706799.aspx/1?using+JOINS+add+columns+of+two+different+tables+in+sql+server+2008</div> <div></div> <div>But now the problem is i want to add this query in .cs file in ASP.NET and m unable to solve this.</div> <div>below i have given the coding of .cs file of trans_mstr</div> <div></div> <div>
usingSystem;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Data.SqlClient;publicpartialclass users_trans_details :System.Web.UI.Page{protectedvoidPage_Load(object sender,EventArgs e){}protectedvoidButton1_Click(object sender,EventArgs e){DateTime dtCurrTime =DateTime.Today;// TO iNSERT A DATE. textbox 4string d = dtCurrTime.ToString();string strImageName =TextBox1.Text.ToString();// To enter the value of text Boxstring dt1 =TextBox5.Text.ToString();string type1 =DropDownList1.SelectedValue;string particular1 =TextBox2.Text.ToString();string dr_cr1 =DropDownList2.SelectedValue;string amt1 =TextBox3.Text.ToString();string balance1 =TextBox4.Text.ToString();SqlConnection con =newSqlConnection();
con.ConnectionString=(@"Data Source=PANKIL-PC\pankil;Initial Catalog=sky;Integrated Security=True");SqlCommand cmd =newSqlCommand();
cmd.CommandText="INSERT INTO trans_mstr(accnt_no,dt,type,particular,dr_cr,amt,balance)"+" VALUES (@accnt_no,@dt,@type,@particular,@dr_cr,@amt,@(update trans_mstr set balance(select curbal from account_mstr where account_mstr.accnt_no=trans_mstr.accnt_no) - amt))";
cmd.CommandType=CommandType.Text;
cmd.Connection= con;SqlParameter accnt_no =newSqlParameter("@accnt_no",SqlDbType.Int);
accnt_no.Value= strImageName.ToString();
cmd.Parameters.Add(accnt_no);SqlParameter dt =newSqlParameter("@dt",SqlDbType.Date);
dt.Value= dt1.ToString();
cmd.Parameters.Add(dt);SqlParameter type =newSqlParameter("@type",SqlDbType.VarChar,1);
type.Value= type1.ToString();
cmd.Parameters.Add(type);SqlParameter particular =newSqlParameter("@particular",SqlDbType.VarChar,30);
particular.Value= particular1.ToString();
cmd.Parameters.Add(particular);SqlParameter dr_cr =newSqlParameter("@dr_cr",SqlDbType.VarChar,1);
dr_cr.Value= dr_cr1.ToString();
cmd.Parameters.Add(dr_cr);SqlParameter amt =newSqlParameter("@amt",SqlDbType.Char,10);
amt.Value= amt1.ToString();
cmd.Parameters.Add(amt);SqlParameter balance =newSqlParameter("@(update trans_mstr set balance=(select curbal from account_mstr where account_mstr.accnt_no=trans_mstr.accnt_no) - amt)",SqlDbType.Char,10);
balance.Value= balance1.ToString();
cmd.Parameters.Add(balance);
con.Open();int result = cmd.ExecuteNonQuery();
con.Close();}}
</div> <div></div> <div></div> <div></div> <div>please help me.</div> <div>thnx</div> <div></div> <div></div>
Regadrs Pankil
Write a stored procedure to insert the data in to the trans_mstr with below insert statement.
INSERT INTO trans_mstr(accnt_no,dt,type,particular,dr_cr,amt,balance) VALUES (@accnt_no,@dt,@type,@particular,@dr_cr,@amt,0)
Following is the update statement for updating the balance.
update trans_mstr set balance =(select curbal from account_mstr where account_mstr.accnt_no=@accnt_no)- amt)
Some thing like below Stored procedure.
CREATE PROCEDURE [dbo].[InsertAndUpdate](@accnt_noasint,@dtas datetime,@typeas varchar(1),@particularas varchar(30),@dr_cras varchar(1),@amtaschar(10))
AS
INSERT INTO trans_mstr(accnt_no,dt,type,particular,dr_cr,amt,balance)
VALUES (@accnt_no,@dt,@type,@particular,@dr_cr,@amt,0)
update trans_mstr set balance =(select curbal from account_mstr where account_mstr.accnt_no=@accnt_no)- amt)
RETURN
Pankil
And the command would be as follows: