I m writing a web service using C#, .NET framework 3.5 in Visual studio 2010. My questions might be basic but I had no luck in making it work.
I could successfully submit my data to SQL db via web service. What I would like to be able to do is while inserting a record/row in one table in SQL db, I would like to be able to insert data into another table let's say table B. Table B has 5 cols. It will get two of the column values from the previous table ( table A that gets inserted via a web service). One column in Table B is Primary key value, rest two columns will have static text values.
Here's the structure for table A
id int (primary key)
CustName varchar(45)
CustAddress varchar(45)
CustContacts varchar(45)
CustQuoteDate varchar(45)
CustLoginId varchar(45)
Table B structure:
id int (primary key)
OrderId varchar(45) NOT NULL //This will get value from table A's Primary key
OrderReqDate [varchar](45) NOT NULL //This will get value from table A's CustQuotDate
OrderStatus [varchar](45) NOT NULL // This will be a static value "Requested"
OrderGroup [varchar](45) NOT NULL // This will be a static value "GGGG"
So I tested out my query for second insertion:
INSERT INTO dbo.TableB(OrderId , currstatus, OrderReqDate, OrderGroup)
SELECT ta.id, 'REQUESTED', ta.CustQuoteDate, 'GGGG' from dbo.TableA ta
where ta.CustLoginId = 'kenw1'
so for each newly added CustLoginId in TableA , the second table ie. TableB also gets a row insert with some fields from tableA.
My question is how/where to edit my web service code below to be able to do second insertion? Your time and effort in helping me out will be greatly appreciated. TIA.
Here's my web service:
[WebMethod(Description = "Adds a Customer Request Form data to the database [TableA].")]
public void AddCustRequestRecord(string CustName, string CustAddress, string custContacts, string custQuoteDate, string CustLoginId)
{
//Static text values for columns in tableB
string OrderStatus = "REQUESTED";
string OrderGroup = "GGGG";
string connectionString =
ConfigurationManager.ConnectionStrings["myDatabase"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
DateTime now = DateTime.Now;
string QuoteDt = now.ToString();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT INTO TableA "
+ "(CustName, CustAddress, custContacts, CustLoginId, custQuoteDate) "
+ "VALUES (@CustName, @CustAddress, @custContacts, @CustLoginId,'" + Quotedt + "')";
cmd.Parameters.AddWithValue("CustName", CustName);
cmd.Parameters.AddWithValue("CustAddress", CustAddress);
cmd.Parameters.AddWithValue("custContacts", custContacts);
cmd.Parameters.AddWithValue("CustLoginId", CustLoginId);
cmd.Parameters.AddWithValue("custQuoteDate", custQuoteDate);
cmd.Connection = conn;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch
{
// Handle the error using your own preferred error-handling method
}
finally
{
conn.Close();
}
}
}