executing Mutiple insert commands from front end

Posted by Krishnamanohar under ASP.NET on 9/19/2013 | Points: 10 | Views : 768 | Status : [Member] | Replies : 3
Hi Experts,
i have some fields on page after entering those fields i have to insert those values in 3 tables when i click on button insert.
here i am not using stored procedure do that one .my main aim is how execute three insert statements for three tables form the front end without using sp.
Please help me urgent............

Thanks in advance
Krish




Responses

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
use 3 different sql commands and execute one by one....
Otherwise use one sql command
SqlCommand cmd = "insert first table"
cmd.execute

//Over write the first insert
cmd = "insert second table"
cmd.execute


//Over write the second insert
cmd = "insert third table"
cmd.execute

con.close

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Deepansak on: 9/19/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

You can use the multiple queries in a single command line with the seperation on semicolon (;)

cmd = "insert into tablex values('a','b','c');insert into tableb values ('a','b','c')"

and we can execute in a single commad execution.

while select query we nned to rs.read and rs.nextrecord

Regards,
Deepan

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/20/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

try this code

SqlConnection objSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Db_Connect"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Bt_Submit_Click(object sender, EventArgs e)
{
string Sqry1 = "Insert into tt1 values('"+ TxtName.Text +"')";
AddRecords(Sqry1);

string Sqry2 = "Insert into tt2 values('" + TxtEmpno.Text + "')";
AddRecords(Sqry2);

string Sqry3 = "Insert into tt3 values(" + Convert.ToInt32(TxtAmount.Text) + ")";
AddRecords(Sqry3);

}
private void AddRecords(string Sqry)
{
objSqlConnection.Open();
SqlCommand sqlcmd = new SqlCommand(Sqry, objSqlConnection);
sqlcmd.ExecuteNonQuery();
objSqlConnection.Close();
}

Mark as Answer if its helpful to you

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response