hello there
i want to create a column in an existing table of my database using C# i mean in my window form 1 textbox and one button is there
i enter some text and clik on the button
then i creates a column named as i write in textbox
my database is sql server database .mdf file
help me out please
Loading

Raja TPosted Feb 26, 2016, 2:30 AM
shivkumar patilPosted Feb 26, 2016, 1:59 AM
i want to create a column in an existing table of my database using C# i mean in my window form 2 textbox and one button is there
i enter some text and click on the button
then i creates a 2 column named as i write in 2 textbox
my database is sql server database .mdf file
help me out please
Satyapriya NayakPosted Jul 14, 2012, 10:03 AM
string str = "alter table [dbo].new add" + textBox1.Text.ToString() + "varchar(50) null";
Change it to
str = "ALTER TABLE [dbo].[new] ADD " + TextBox1.Text.ToString() + " Varchar(50) NULL ";
Note
1.put space between add and " .
2.put space between " and varchar(50).
3.change [dbo].new to [dbo].[new].
Thanks
praveen mauryaPosted Jul 14, 2012, 9:49 AM
i am using the following code
using System.Data.SqlClient;
System.Data.SqlClient.SqlConnection cn;
{ (button click event)
}
and it shows error that is
Incorrect syntax near 'addexfvarchar'
where exf is the name i am entering in my textbox for new table
Satyapriya NayakPosted Jul 14, 2012, 7:58 AM
Kunal code is working fine
using System.Data.SqlClient;
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "ALTER TABLE [dbo].[Test] ADD " + TextBox1.Text.ToString() + " Varchar(50) NULL ";
com = new SqlCommand(str,con);
com.ExecuteNonQuery();
con.Close();
}
Please accept kunal's answer
Thanks
Kunal VaishyaPosted Jul 14, 2012, 5:34 AM
Set Command String in SqlCommand and Execute Command
ALTER TABLE [dbo].[DatabaseName] ADD ColumnName Varchar(50) NULL
ALTER TABLE [dbo].[Nothwind] ADD "+ textbox1.Text.ToString() +" Varchar(50) NULL
just do it column will created