Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
yamoo
Starting Member
11 Posts |
Posted - 2013-08-05 : 14:26:15
|
public partial class _Default : System.Web.UI.Page{SqlConnection con = new SqlConnection();SqlCommand com = new SqlCommand();SqlDataAdapter dap = new SqlDataAdapter();DataSet ds = new DataSet();DataTable dt = new DataTable();DataTable dt1 = new DataTable();DataRow dr;protected void Page_Load(object sender, EventArgs e){con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString;com.CommandText = "select employeeid from employees order by employeeid";com.Connection = con;dap.SelectCommand = com;dt.TableName = "employees";ds.Tables.Add(dt);dap.Fill(ds.Tables["employees"]);for (int i = 0; i < dt.Rows.Count; i++){cobedi.Items.Add(dt.Rows[i][0].ToString());cobdel.Items.Add(dt.Rows[i][0].ToString());}}protected void btnadd_Click(object sender, EventArgs e){con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString;com.CommandText = "insert into employees (FirstName,LastName) values (@f,@l)";com.Parameters.Add("@f", SqlDbType.NVarChar, 30).Value = txtfst.Text;com.Parameters.Add("@l", SqlDbType.NVarChar, 30).Value = txtlst.Text;com.Connection = con;dap.InsertCommand = com;dr = ds.Tables["employees"].NewRow();dr["FirstName"]=txtfst.Text;dr["LastName"]=txtlst.Text;ds.Tables["employees"].Rows.Add(dr);dap.Update(ds.Tables["employees"]);}when i run this code and try add a new row ...... messaage appear in webpage :Server Error in '/R.Ado' Application.Column 'FirstName' does not belong to table employees.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.ArgumentException: Column 'FirstName' does not belong to table employees.Source Error:Line 91: dap.InsertCommand = com;Line 92: dr = ds.Tables["employees"].NewRow();Line 93: dr["FirstName"]=txtfst.Text;Line 94: dr["LastName"]=txtlst.Text;Line 95: ds.Tables["employees"].Rows.Add(dr);Source File: f:\C# APPLICATION\asp\R.Ado\Default.aspx.cs Line: 93Stack Trace:[ArgumentException: Column 'FirstName' does not belong to table employees.]System.Data.DataRow.GetDataColumn(String columnName) +1775189System.Data.DataRow.set_Item(String columnName, Object value) +12_Default.btnadd_Click(Object sender, EventArgs e) in f:\C# APPLICATION\asp\R.Ado\Default.aspx.cs:93System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565Version Information: Microsoft .NET Framework Version:2.0.50727.4984; ASP.NET Version:2.0.50727.4971 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-05 : 14:35:35
|
This is because in your Page_Load you are loading only employeeid, so your data table gets created with just that one column. Then in the btnadd_Click you are trying to set the FirstName and LastName columns to some values. But those columns don't exist in the table. Depending on what you want, one way to fix it would be to add the two columns to your Page_Load....con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString;com.CommandText = "select employeeid,FirstName, LastName from employees order by employeeid"; Taht assumes that the employees table in the database has those two columns with those names. |
|
|
yamoo
Starting Member
11 Posts |
Posted - 2013-08-05 : 15:56:33
|
thank you very very much mr.james |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-05 : 16:50:30
|
You are very welcome. Glad to help. |
|
|
|
|
|
|
|