By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,966 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,966 IT Pros & Developers. It's quick & easy.

System.Data.SqlClient.SqlException

P: 3
Please help me

" System.Data.SqlClient.SqlException: Prepared statement '(@Name varchar(100),@Describes varchar(100),@Money char(10))Upda' expects parameter @Describes, which was not supplied. at System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Assignment.rose.dtgFlower_UpdateCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\assignment1\rose.ascx.cs:line 111Error:Cannot update "

This is the code
Expand|Select|Wrap|Line Numbers
  1. "namespace Assignment
  2. {
  3.     using System;
  4.     using System.Data;
  5.     using System.Drawing;
  6.     using System.Web;
  7.     using System.Web.UI.WebControls;
  8.     using System.Web.UI.HtmlControls;
  9.     using System.Data.SqlClient;
  10.  
  11.     /// <summary>
  12.     ///        Summary description for rose.
  13.     /// </summary>
  14.     public class rose : System.Web.UI.UserControl
  15.     {
  16.         protected System.Web.UI.WebControls.DataGrid dtgFlower;
  17.  
  18.         private void Page_Load(object sender, System.EventArgs e)
  19.         {
  20.             // Put user code to initialize the page here
  21.             SqlConnection sqlcon=new SqlConnection("server=(local);uid=sa;pwd=12345;database=Assignment");
  22.             SqlCommand sqlcom=new SqlCommand("select * from ProductFlower",sqlcon);
  23.             sqlcon.Open();
  24.             SqlDataReader sqlreader;
  25.             sqlreader=sqlcom.ExecuteReader();
  26.             if(sqlreader.HasRows)
  27.             {
  28.                 dtgFlower.DataSource=sqlreader;
  29.                 dtgFlower.DataBind();
  30.             }
  31.             else
  32.             {
  33.                 sqlcon.Close();
  34.             }
  35.             if(!IsPostBack)
  36.                  BindGrid();
  37.         }
  38.  
  39.         #region Web Form Designer generated code
  40.         override protected void OnInit(EventArgs e)
  41.         {
  42.             //
  43.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  44.             //
  45.             InitializeComponent();
  46.             base.OnInit(e);
  47.         }
  48.  
  49.         /// <summary>
  50.         ///        Required method for Designer support - do not modify
  51.         ///        the contents of this method with the code editor.
  52.         /// </summary>
  53.         private void InitializeComponent()
  54.         {
  55.             this.dtgFlower.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgFlower_CancelCommand);
  56.             this.dtgFlower.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgFlower_EditCommand);
  57.             this.dtgFlower.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgFlower_UpdateCommand);
  58.             this.dtgFlower.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgFlower_DeleteCommand);
  59.             this.Load += new System.EventHandler(this.Page_Load);
  60.  
  61.         }
  62.         #endregion
  63.  
  64.         protected System.Web.UI.WebControls.Label lblMessage;
  65.  
  66.  
  67.         SqlConnection sqlcon=new SqlConnection("server=(local);uid=sa;pwd=12345;database=Assignment");
  68.  
  69.         public void BindGrid()
  70.         {
  71.             SqlDataAdapter sqlda=new SqlDataAdapter("select * from ProductFlower",sqlcon);
  72.             DataSet ds=new DataSet();
  73.             sqlda.Fill(ds,"ProductFlower");
  74.             dtgFlower.DataSource=ds.Tables["ProductFlower"].DefaultView;
  75.             dtgFlower.DataBind();
  76.         }
  77.  
  78.         private void dtgFlower_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  79.         {
  80.             dtgFlower.Columns[0].HeaderText="Cancel";
  81.             dtgFlower.EditItemIndex=-1;
  82.             BindGrid();
  83.         }
  84.  
  85.         //private TextBox txtDescribes,txtMoney;
  86.  
  87.         private void dtgFlower_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  88.         {
  89.  
  90.             dtgFlower.Columns[5].HeaderText="Update";
  91.             int ID = Int32.Parse(dtgFlower.DataKeys[e.Item.ItemIndex].ToString());    
  92.             string strUpdate="Update ProductFlower set Name=@Name,Describes=@Describes,Money=@Money where ProductID=ID";
  93.             SqlCommand sqlcom=new SqlCommand(strUpdate,sqlcon);
  94.  
  95.             //sqlcom.Parameters.Add(new SqlParameter("@ProductID",SqlDbType.Int,4));
  96.             //sqlcom.Parameters.Add(new SqlParameter("@Image",SqlDbType.VarChar,100));
  97.             sqlcom.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar,100));
  98.             sqlcom.Parameters.Add(new SqlParameter("@Describes",SqlDbType.VarChar,100));
  99.             sqlcom.Parameters.Add(new SqlParameter("@Money",SqlDbType.Char,10));
  100.  
  101.             //sqlcom.Parameters["@ProductID"].Value=dtgFlower.DataKeys[(int)e.Item.ItemIndex];
  102.             //sqlcom.Parameters["@Image"].Value=((TextBox)e.Item.Cells[2].Controls[0]).Text;
  103.             sqlcom.Parameters["@Name"].Value=((TextBox)e.Item.Cells[3].Controls[0]).Text;
  104.             sqlcom.Parameters["@Describes"].Value=(e.Item.FindControl("TextBox"));
  105.             sqlcom.Parameters["@Money"].Value=(e.Item.FindControl("TextBox"));
  106.             //sqlcom.Parameters["@Describes"].Value=((TextBox)e.Item.Cells[4].Controls[0]).Text;
  107.             //sqlcom.Parameters["@Money"].Value=((TextBox)e.Item.Cells[5].Controls[0]).Text;
  108.             sqlcon.Open();
  109.             try
  110.             {
  111.                 sqlcom.ExecuteNonQuery();
  112.                 lblMessage.Text="Completed Successful";
  113.                 dtgFlower.EditItemIndex=-1;
  114.             }
  115.             catch(SqlException exc)
  116.             {
  117.                 if(exc.Number==2627)
  118.                     lblMessage.Text="Error";
  119.                 else
  120.                     lblMessage.Text=exc.ToString()+"Error:Cannot update";
  121.             }
  122.             sqlcon.Close();
  123.             BindGrid();
  124.         }
  125.  
  126.         private void dtgFlower_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  127.         {
  128.             dtgFlower.Columns[5].HeaderText="Edit";
  129.             dtgFlower.EditItemIndex=(int)e.Item.ItemIndex;
  130.             BindGrid();
  131.         }
  132.  
  133.         private void dtgFlower_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  134.         {
  135.             dtgFlower.Columns[6].HeaderText="Delete";
  136.             string strDelete="Delete from ProductFlower where ProductID=@ProductID";
  137.             SqlCommand sqlcom=new SqlCommand(strDelete,sqlcon);
  138.             sqlcom.Parameters.Add(new SqlParameter("@ProductID",SqlDbType.Int,4));
  139.             sqlcom.Parameters["@ProductID"].Value=dtgFlower.DataKeys[(int)e.Item.ItemIndex];
  140.             sqlcon.Open();
  141.             try
  142.             {
  143.                 sqlcom.ExecuteNonQuery();
  144.                 lblMessage.Text="Completed Successful";
  145.             }
  146.             catch(SqlException)
  147.             {
  148.                 lblMessage.Text="Error:Cannot delete";
  149.             }
  150.             sqlcon.Close();
  151.             BindGrid();
  152.         }
  153.  
  154.  
  155.  
  156.     }
  157. }
  158.  
  159.  
This is the database
"CREATE TABLE [dbo].[ProductFlower] (
[ProductID] [int] IDENTITY (1, 1) NOT NULL ,
[Image] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Name] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Describes] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Money] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY] "
Dec 15 '07 #1
Share this Question
Share on Google+
1 Reply


jhardman
Expert 2.5K+
P: 3,405
This isn't written in VBScript. What language and what version of ASP are you using?

My first response would be to stop using complex SQL, and just dump the data you want to manipulate into a recordSet, then manipulate it how you want then update the db from the recordset. Of course, this may not be doable with all versions of ASP, so my first question is of utmost importance.

Jared
Dec 17 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Browse more ASP / Active Server Pages Questions on Bytes