insert value in datatable and pass it into a mehod
i just use this code to insert value in datatable and pass this table in method look code---
public class RoomBO
{
DataTable dt;
RoomDal objRoom = new RoomDal();
public int insert(int room,string bedType, int bed, int charge)
{
//adds the details of the new room
dt = new DataTable();
dt.Columns.Add(new DataColumn("Column1"));
dt.Columns.Add(new DataColumn("Column2"));
dt.Columns.Add(new DataColumn("Column3"));
dt.Columns.Add(new DataColumn("Column4"));
DataRow dr;
dr = dt.NewRow();
dr["Column1"] = room;
dr["Column2"] = bedType;
dr["Column3"] = bed;
dr["Column4"] = charge;
objRoom.Save(dt);
return 1;
}
****on data access layer****
public int Save(DataTable dataTable) //adds the details of the new room
{
ada = new SqlDataAdapter("room_insert", con);
//DataRow r= dataTable.NewRow();
ada.SelectCommand.CommandType = CommandType.StoredProcedure;
ada.SelectCommand.Parameters.AddWithValue("@room_no",
Convert.ToInt32(dataTable.Rows[0][0]));
ada.SelectCommand.Parameters.AddWithValue("@room_type",
Convert.ToString(dataTable.Rows[0][1].ToString()));
ada.SelectCommand.Parameters.AddWithValue("@no_bed",
Convert.ToInt32(dataTable.Rows[0][2]));
ada.SelectCommand.Parameters.AddWithValue("@char开发者_C百科ge",
Convert.ToInt32(dataTable.Rows[0][3]));
ds = new DataSet();
ada.Fill(ds);
return -1;
}
i am not getting any error but value is not insert to my database
For the data-layer:
Try changing to
ada = new SqlAdapter( "room_insert( @room_no, @room_type, @no_bed, @charge )" );
Checking the ctor's for SqlAdapter it says:
public SqlDataAdapter (
SqlCommand selectCommand
)
So the command must be complete.
hth
Mario
I did some more research: It doesn't work out as easily...
See http://support.microsoft.com/kb/308507 for details. You need to specify an InsertCommand and the use ada.Update .
My understanding is that a DataAdapter always refes to the same DataSet. I think it will be tricky to cheat the DataAdapter to work correctly...
Why do you want to use a DataAdaper at all (since you do not use it on a once retrieved DataSet?
You could just
SqlCommand cmd = new SqlCommand( "room_insert( @room_no, @room_type, @no_bed, @charge )" );
cmd.Parameters.Add("@room_no", <datatype> ).Value = ...
return 1 == cmd.ExecuteNonQuery();
hth
Mario
精彩评论