Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi frnds i need to rename my column name in an database Dynamically Here is my code but i got error as Invalid Column name Pls do help me

C#
try
          {


              foreach (DataGridViewRow row in dataGridView1.Rows)
              {

                  foreach (DataGridViewCell cell in row.Cells)
                  {
                      value1 = row.Cells[0].Value.ToString();
                      value2 = row.Cells[1].Value.ToString();

                      cmd = new SqlCommand("insert into Classify2 (["+comboBox1.SelectedItem.ToString()+"],["+comboBox2.SelectedItem.ToString()+"]) values (@A,@B)", cn.con);
                      cmd.Parameters.AddWithValue("@A", value1.ToString());
                      cmd.Parameters.AddWithValue("@B", value2.ToString());
                      cmd.ExecuteNonQuery();
                      cmd.Dispose();
                      //MessageBox.Show("Execute Process!!");
                  }

              }


          }
          catch (Exception error)
          {

          }


Thank you
Posted
Comments
usha C 15-Jul-13 3:53am    
The Combo box values contain the field names....leave of that i need to rename my database column name using c# code...is it possible to do???
berrymaria 15-Jul-13 3:56am    
Have you tried this? Example:

dataTable.Columns["Marks"].ColumnName = "SubjectMarks";
berrymaria 15-Jul-13 3:53am    
comboBox1 and comboBox2 contains the name of the columns in the Classify2? What is your problem with that? Why do you need to rename your database column names?
Gishisoft 15-Jul-13 3:53am    
In order for you to change/replace or rename the column of your database table. you need to know first the basic syntax of sql. you may refer this link "http://www.techonthenet.com/sql/tables/alter_table.php" on how to rename/change/replace the column name. If you know that it is easier for you to apply it in your c# codes.

SQL ALTER TABLE - Modifying column(s) in a table
Gishisoft 15-Jul-13 3:56am    
SQL ALTER TABLE - Modifying column(s) in a table

Syntax #1

To modify a column in an existing table, the SQL ALTER TABLE syntax is:

ALTER TABLE table_name
MODIFY column_name column_type;

For example:

ALTER TABLE supplier
MODIFY supplier_name varchar2(100) not null;

This will modify the column called supplier_name to be a data type of varchar2(100) and force the column to not allow null values.

Syntax #2

To modify multiple columns in an existing table, the SQL ALTER TABLE syntax is:

ALTER TABLE table_name
MODIFY (column_1 column_type,
column_2 column_type,
...
column_n column_type);

For example:

ALTER TABLE supplier
MODIFY (supplier_name varchar2(100) not null,
city varchar2(75));

This will modify both the supplier_name and city columns.

1 solution

You can use
SQL
sp_rename 
to do that.
 
Share this answer
 
v2
Comments
usha C 15-Jul-13 5:18am    
oops am geting dis error "Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong."
zenspace 15-Jul-13 5:29am    
show your code.
usha C 15-Jul-13 5:43am    
cmd = new SqlCommand("SP_RENAME 'Classify2.[A]' , '[newtable]', 'COLUMN'", cn.con);
cmd.ExecuteNonQuery();
cmd.Dispose();

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