Hi tanweeruddinb...,
a list.
Refer below updated code.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<string> columns = new List<string>() { "u_ip", "u_name" };
List<object> data = new List<object>() { 110654965406 };
List<string> whereColumns = new List<string>() { "student_id", "teacher_id" };
List<string> whereValues = new List<string>() { "10", "12" };
string tableName = "Newuser";
testqueryUpdate(null, columns, data, tableName, whereColumns, whereValues);
}
}
public string testqueryUpdate(string Transaction, List<string> columns, List<object> data, string tableName, List<string> whereColumns, List<string> whereValues)
{
var strCol = string.Join(",", columns);
var strParam = string.Join(",", columns.Select(r => "@" + r));
var sql = "UPDATE [" + tableName + "] SET ";
for (int i = 0; i < columns.Count; i++)
{
if (i == 0)
{
sql += "[" + columns[i] + "] = @" + columns[i];
}
else
{
sql += ", [" + columns[i] + "] = @" + columns[i];
}
}
sql += " where ";
for (int i = 0; i < whereColumns.Count; i++)
{
if (i == 0)
{
sql += "[" + columns[i] + "] = @" + columns[i];
}
else
{
sql += " AND [" + columns[i] + "] = @" + columns[i];
}
}
SqlConnection con = new SqlConnection("");
SqlCommand command = new SqlCommand();
command.CommandText = sql;
command.CommandType = CommandType.Text;
for (int i = 0; i < columns.Count; i++)
{
command.Parameters.AddWithValue("@" + columns[i], data[i]);
}
for (int i = 0; i < whereColumns.Count; i++)
{
command.Parameters.AddWithValue("@" + whereColumns[i], whereValues[i]);
}
command.Connection = con;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (command.ExecuteNonQuery() > 0)
{
con.Close();
return "SUCCESS";
}
else
{
con.Close();
return "Fail";
}
}