Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai Sir,

How to add new column for the existing database table by inserting a column from the front end in .Net?
Posted
Comments
Shanalal Kasim 22-May-15 5:34am    
Are you using code first approach
Kanaparthi Sureshma Reddy 22-May-15 5:38am    
I am using 3-Tier Architecture
Tomas Takac 22-May-15 5:35am    
What do you mean? Adding new column to and existing table or inserting data into an existing table from front-end? These are two different things.
Kanaparthi Sureshma Reddy 22-May-15 5:43am    
Actually in my database I have a table with columns sub1,sub2,sub3.
Now I want to add a new column(sub4) from front end, When I click insert button in frontend that sub4 will be added to table like sub1,sub2,sub3,sub4.
What is the stored procedure and c# code for this?
Tomas Takac 22-May-15 5:52am    

1 solution

I am not sure whether this would correct approach or not.


As Tomas suggested, use ALTER TABLE and use it inside a store procedure.

This store procedure must have three input parameters 1. Column Name 2. Data type of column 3. Nullable or not.

Now decorate your alter table query with this parameters and make the SP ready.


In your UI, one text box where Column Name user will give, One Dropdown for data type and one checkbox for nullable. Get all those values from UI and pass it to BA->DA layer.

There call that Store procedure you created and passs this as input parameters.


Hopefull this will works.

Edited:
CREATE PROCEDURE MYPROC
    @COLUMNNAME  nvarchar(50),
    @DATATYPE  nvarchar(50) = 'nvarchar(50)'    
AS
BEGIN
declare @cmd nvarchar(max)
    
    set @cmd = 'ALTER TABLE TEST
    ADD' + @COLUMNNAME + ' ' + @DATATYPE

    exec sp_executesql @cmd
END;

ADO.NET Component

Use a method with paramater which will be set in your Ui, and pass the values to BA and DA method. In this DA method use the below code. Add/Remove as per your requirement. For SQL data type use a variable and fill/assign different SQL data type based on condition and use it in @DATATYPE parameter add value place.

SqlConnection con = null;
try
            {
         string ConnectionString = "server=XXX;uid=sa;"+
                    "pwd=XXXX; database=northwind";
         con = new SqlConnection(ConnectionString);
         con.Open()
        SqlCommand cmdProc = new SqlCommand("MYPROC", con);
        cmdProc.CommandType = CommandType.StoredProcedure;
        cmdProc.Parameters.AddWithValue("@COLUMNNAME", "Sub4"); // Pass the text value  you got from UI here.
        cmdProc.Parameters.AddWithValue("@DATATYPE", System.Data.SqlDbType.NVarChar);      // Add proper type as per your requirement. 
        cmdProc.ExecuteNonQuery();
}
 
Share this answer
 
v6
Comments
Tomas Takac 22-May-15 6:11am    
One note: the alter table will be dynamic SQL. Otherwise this more info than I was willing to give so +5.
Pradip Kumar Sen 22-May-15 7:41am    
Thanks Tomas. I have added a SP for this. See if this is okay or not.

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