0

Im new to ASP.net webforms.Im having a event page,in which i have a field to add sales channel heads mail id.when i click on the plus button i will be able to add more than one sales channels head.

enter image description here

For inserting the form values into the database im using Stored procedure.and its inserting the records with one sales channel head email id.

I want to know how i can write a stored procedure for inserting dynamic textbox values into sql server for the same record(That is the id and event name should be same).

This is my stored procedure

CREATE PROCEDURE SPInsertEvent
   @eventName varchar(200),
   @eventDate date,
   @costPerHead varchar(200),
   @totalCost varchar(200),
   @salesChannelHeads varchar(200),
   @salesManagers varchar(200),
   @eventCreationTime datetime
AS
BEGIN
   SET NOCOUNT ON
-- Insert statements for procedure here
   INSERT INTO dbo.hp_event
   (event_name, event_date, cost_per_head, total_cost, sales_channel_head,    sales_manager,    event_creation_time)
   VALUES
   (@eventName, @eventDate, @costPerHead, @totalCost, @salesChannelHeads, @salesManagers, @eventCreationTime)
END

This is my ASP.net function

SqlCommand cmd = new SqlCommand("SPInsertEvent", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("EventName", txtEventName.Text);
cmd.Parameters.AddWithValue("EventDate", Convert.ToDateTime(txtEventDate.Text));
cmd.Parameters.AddWithValue("CostPerHead", txtTotCostPerHead.Text);
cmd.Parameters.AddWithValue("TotalCost", txtTotalCostEvent.Text);
cmd.Parameters.AddWithValue("SalesChannelHead", txtSalesChannelHead.Text);
cmd.Parameters.AddWithValue("SalesManager", txtSalesManagers.Text);
cmd.Parameters.AddWithValue("EventCreationTime", DateTime.Now);
conn.Open();
int k = cmd.ExecuteNonQuery();
if (k != 0)
{
  string message = "Event added successfully.";
  string script = "window.onload = function(){ alert('";
  script += message;
  script += "')};";
  ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
}
conn.Close();
Thom A
  • 88,727
  • 11
  • 45
  • 75
Teena
  • 155
  • 1
  • 15
  • Side question. You have the parameters `@salesChannelHeads` and `@salesManagers`. The names are plural, yet you are inserting it as a single row; which *implies* a denormalised design. – Thom A Jun 10 '20 at 10:44
  • Side note, I suggest you [avoid AddWithValue](https://www.dbdelta.com/addwithvalue-is-evil/), although this isn't a major issue with a stored procedure call. – Dan Guzman Jun 10 '20 at 10:46
  • @Larnu Hi salesChannelHeads and salesManagers are two different fields.i will be having multiple salesChannelHeads mail id,for that i have given the plus button to add more text boxes for Sales Channel Heads. – Teena Jun 10 '20 at 10:51
  • @DanGuzman im new to this , i will change that part to parameters.my question is how i can insert the values using stored procedures. – Teena Jun 10 '20 at 10:54
  • Side question - why is everything a string in your mind? totalCost and costPerHead sound very much like numeric values. Columns with names like "Id" are often int. Regardless, Erland has much useful information about using ["arrays" in tsql](http://www.sommarskog.se/arrays-in-sql-2005.html) as well as using [TVPs](http://www.sommarskog.se/arrays-in-sql-2008.html). Still not clear what your goal is but these are likely good places to start. – SMor Jun 10 '20 at 11:22
  • @SMor its because,the input fields will be accepting text characters,to do all the math calculations i can just type cast it into int. What i need is a stored procedure for inserting saleschannel heads email id(txtSalesChannelHead,txtSalesChannelHead1,txtSalesChannelHead2) into the sql server table with same id,that is there will be duplicate rows in the table . – Teena Jun 11 '20 at 01:25
  • Your question does not tell on how the email ids will be added? comma separated list or separate ids every time you click the "+" something like again opening the text box? If they are separate why can't store them in separate table and reference the EventName? – MBB Jun 11 '20 at 16:27
  • @mahesh_b it will be seperate ids ,im quite new to this.storing them in a seperate table means ,creating a temp table and storing them. – Teena Jun 12 '20 at 01:26

2 Answers2

0

You said in the comments "What i need is a stored procedure for inserting saleschannel heads email id(txtSalesChannelHead,txtSalesChannelHead1,txtSalesChannelHead2) into the sql server table with same id,that is there will be duplicate rows in the table". Handling a dynamic number of inputs like that is not best done in a stored procedure, from what i can see of your scenario. The easier way is to run the insert procedure from your .NET code once for each textbox. Now I don't know how your textboxes are being added, so I can't tell you how to get the set of textbox values, but once you have it, a simple foreach loop will let you run the stored procedure once for each of them.

erastl
  • 421
  • 4
  • 9
  • Thanks for your reply.So what you are saying is if more than one SalesChannelHeads MailId is there,i should put that inside a forloop? – Teena Jun 12 '20 at 01:30
  • Yep, if you have more than 1 textbox, just loop through them and run the procedure once for each saleschannelhead. – erastl Jun 12 '20 at 12:29
0

Instead of storing all the list of email ids for the given event in one table, I would suggest you to store them in separate table and you can reference them from the hp_event table whenever you need. So your database design should be some thing like below where eventid of hp_eventSalesManagers references eventId of hp_event -

enter image description here

To make this design work you can make use of Table Valued Parameters in ADO.NET and follow the below steps:

  1. Create a User Defined Type -

      CREATE TYPE [dbo].[ChannelHeads] As Table
      (    
        EmailIds VARCHAR(50)
      )
    
  2. Whenever you click button populate a new Data Table(I am using Session to keep track of the previous data), below is the sample code:

       protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["DataTable"] == null)
            {
                dataTable = new DataTable();
                dataTable.Columns.Add("EmailIds", typeof(string));
                Session.Add("DataTable", dataTable);
            }
            else
            {
                //If yes then get it from current session
                dataTable = (DataTable)Session["DataTable"];
            }
    
            DataRow dt_row;
            dt_row = dataTable.NewRow();
            dt_row["EmailIds"] = name.Text;
            dataTable.Rows.Add(dt_row);
        }
    
  3. When submitting to data base add the below parameter(See the way I am passing the data table to DB):

       SqlParameter parameterSalesChannelHeads = new SqlParameter();
       parameterSalesChannelHeads.ParameterName = "@salesChannelHeads";
       parameterSalesChannelHeads.SqlDbType = System.Data.SqlDbType.Structured;
       parameterSalesChannelHeads.Value = (DataTable)Session["DataTable"];
       parameterSalesChannelHeads.TypeName = "dbo.ChannelHeads";
       cmd.Parameters.Add(parameterSalesChannelHeads);
    

    Change all your parameters in above format just to make sure you are using Parameters.Add instead of Parameters.AddWithValue as mentioned here

  4. Finally change the procedure as below to populate the tables, below is one of the way, you can enable error handling and improve the code:

      ALTER PROCEDURE SPInsertEvent
        @eventName varchar(200),
        @eventDate datetime,
        @costPerHead varchar(200),
        @totalCost varchar(200),
        @salesChannelHeads As [dbo].[ChannelHeads] Readonly,
        @salesManagers varchar(200),
        @eventCreationTime datetime
     AS
      BEGIN 
      SET NOCOUNT ON
      DECLARE @eventID INT
    
      -- Insert statements for procedure here
      INSERT INTO dbo.hp_event
      (event_name, eventDate, costPerHead, totalCost, eventCreationTime, 
      salesManagers)
      VALUES
      (@eventName, @eventDate, @costPerHead, @totalCost,@eventCreationTime, 
      @salesManagers)
    
      SET @eventID = SCOPE_IDENTITY()
    
      INSERT INTO dbo.hp_eventSalesManagers
       (eventid,event_name,salesChannelHeads)
      SELECT @eventID, @eventName, EmailIds
       FROM 
       @salesChannelHeads 
    
    END
    

Finally change the data types of the fields accordingly as mentioned in the comment section for better clarity and usages.

MBB
  • 1,635
  • 3
  • 9
  • 19
  • Thank you mahesh for the detailed explanation. – Teena Jun 13 '20 at 05:28
  • i have one more doubt,what if im using comma separated values?i have seen that ,need to create a splitstring function first and i need to call that function,but im quite confused how and where to call the splitstring function.Could you please help me out. – Teena Jun 13 '20 at 12:50
  • You need not to create new split string function. Use the existing split function on ',' charecter and add each one of them in datatable before calling procedure. https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netcore-3.1 https://stackoverflow.com/questions/11519543/how-to-add-comma-separated-string-into-datatable-in-c – MBB Jun 13 '20 at 12:56