SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 problem in ADO ?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

yamoo
Starting Member

Egypt
11 Posts

Posted - 08/05/2013 :  14:26:15  Show Profile  Reply with Quote
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: 93

Stack Trace:


[ArgumentException: Column 'FirstName' does not belong to table employees.]
System.Data.DataRow.GetDataColumn(String columnName) +1775189
System.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:93
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Version Information: Microsoft .NET Framework Version:2.0.50727.4984; ASP.NET Version:2.0.50727.4971

James K
Flowing Fount of Yak Knowledge

3719 Posts

Posted - 08/05/2013 :  14:35:35  Show Profile  Reply with Quote
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.
Go to Top of Page

yamoo
Starting Member

Egypt
11 Posts

Posted - 08/05/2013 :  15:56:33  Show Profile  Reply with Quote
thank you very very much mr.james
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3719 Posts

Posted - 08/05/2013 :  16:50:30  Show Profile  Reply with Quote
You are very welcome. Glad to help.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000