I create a web form that contains: Dropdownlist, texbox and rename button. The general idea is that Dropdownlist contains list of column names of one table in my database. Then the user select one of these names and enter the new name in the textbox. After that, he click the rename button. The result is rename the selected column in my database. My code is work well. And it is give exact result. see my code:
protected void Button1_Click(object sender, EventArgs e)
{
string conString = @"Data Source=FATTO-TOSH\SQLEXPRESS;Initial Catalog=Positions;Integrated Security=True";
if (DropDownList1.SelectedIndex == 0)
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("sys.sp_rename", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@objname", "PositionsReq.skill1")
.SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("@newname", name.Text)
.SqlDbType = SqlDbType.NVarChar;
cmd.ExecuteNonQuery();
}
if (DropDownList1.SelectedIndex == 1)
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("sys.sp_rename", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@objname", "PositionsReq.skill2")
.SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("@newname", name.Text)
.SqlDbType = SqlDbType.NVarChar;
cmd.ExecuteNonQuery();
}
if (DropDownList1.SelectedIndex == 2)
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("sys.sp_rename", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@objname", "PositionsReq.skill3")
.SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("@newname", name.Text)
.SqlDbType = SqlDbType.NVarChar;
cmd.ExecuteNonQuery();
}
}
My question is: The modification achieved only one time (regard to one column) because when I change the name from skill1 to Sk for example. May in other time, the user want to modify Sk to other name. the code doesn't work because it is initialize as column name is skill1 only. do you have an idea how to generalize the code to work whatever the name of the column? thank you