Hello Holokai
I don't often discuss ASP.Net in this forum, mostly because I find the help I get the pretty bad... There are many nice ways of getting your code a bit compressed. That thing about one line is really not relevant since then you have fewer customization options. In order to get that into one line in C# you could create a data access layer and an insert function that takes your strings as parameters, but then in your business layer you could simply call the method, e.g.
Code:
private objDAL dal = new objDAL();
if(dal.Insert("string one", [...], "string n")) lblAnswer.Text = "Update successful";
but then in your data access layer you'd have to instantiate the connection and the command. I'll paste a function from an image gallery into the code sketch below:
Code:
namespace h {
class h.e {
static int concurrentCons;
//and here are the rest of the variables and the constructor
public dal() {
//constructor
}
public static List<Photo> GetPhotos(int AlbumID) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString)) {
using (SqlCommand command = new SqlCommand("GetPhotos", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID));
bool filter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators"));
command.Parameters.Add(new SqlParameter("@IsPublic", filter));
connection.Open();
List<Photo> list = new List<Photo>();
using (SqlDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {
Photo temp = new Photo(
(int)reader["PhotoID"],
(int)reader["AlbumID"],
(string)reader["Caption"]);
list.Add(temp);
}
}
return list;
}
}
}
} }
Alright, that's the structure of it. A few buts... (haha...) This code isn't for what you wanted. It's for getting, but it's the same paradigm. It all depends on how scalable you want to do it and how good you want your application architecture to be... If you don't care for stored procedures or don't know how to use them, just write the SQL string in your command - then insert the strings via parameters. This takes care of SQL injections while you're at it... Then execute it.
But if you do it like this you'll be able to call it from any codebehind (presentation layer) you want and you can do it with only one line. The line corresponding to this get function would be
Code:
//get album
try {
List<Photo> album = GetPhotos(Request.QueryString["id"]);
}
catch { //couldn't be done }