tags:

views:

39

answers:

2

HI, I have a web form which has 23 textbox's , I need to pass it to a function, which has SQL command to insert the values of 23 textbox to the table..

Is there a better way using DataSet and SQLDataAdapter? Becuase if not then i need to pass all the 23 vlues in the function like

//function to insert textbox values to the table

public int createRecord(val1,val2,val3,....val23)
{
   string query = "insert into tablename values (.23 values...)";
   sqlCommand command = new sqlCommand(query,connectionstring);
..
}

please suggest me atleast a good method or give a good link which can solve my problem..

+1  A: 

If you are inserting a record and all 23 values are necessary, then there's not really a better way.

However, it'd be better to use a stored procedure and pass the parameters to the stored procedure instead of using hardcoded SQL in your application, which is always a bad idea.

Something like this:

SqlCommand cmd  = new SqlCommand("YourSPName", conn);
cmd.CommandType = CommandType.StoredProcedure;

// add the parameters the SP needs
cmd.Parameters.Add(new SqlParameter("@Parm1", parm1Value));
cmd.Parameters.Add(new SqlParameter("@Parm2", parm2Value));
.
.

// execute the SP
cmd.ExecuteNonQuery();
dcp
it cannot be done in any by using dataset , where if i say Dataset ds = new Dataset(); //and include ds.tables[0].row[0][0] = textbox.text; // and then pass in the function as the parameter say "ds"? i tried it but got error saying indexing exception :(
Nagaraj Tantri
@Nagaraj - If you are getting an indexing exception, it means that you probably don't have the row or column created. You can examine these types of things in the debugger if you are having troubles. First, make sure ds.tables[0] exists. If that works, then check ds.tables[0].rows[0]. That gets you the row. ds.tables[0].rows[0][0] gets you the column value. But if the row doesn't exist, or if the row exists but has no columns, then that could be causing your problem.
dcp
A: 

You may re-write your function to the following, and pass a list of values instead of 23 parameters.

Replace your function to

public int createRecord(your_type[] list)
{
   // .. building insert statement from the list of values.
}
Michael Pakhantsov