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
"namespace Assignment
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for rose.
/// </summary>
public class rose : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid dtgFlower;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection sqlcon=new SqlConnection("server=(local);uid=sa;pwd=12345;database=Assignmen t");
SqlCommand sqlcom=new SqlCommand("select * from ProductFlower",sqlcon);
sqlcon.Open();
SqlDataReader sqlreader;
sqlreader=sqlcom.ExecuteReader();
if(sqlreader.HasRows)
{
dtgFlower.DataSource=sqlreader;
dtgFlower.DataBind();
}
else
{
sqlcon.Close();
}
if(!IsPostBack)
BindGrid();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dtgFlower.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dtgFlower_CancelCommand);
this.dtgFlower.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dtgFlower_EditCommand);
this.dtgFlower.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dtgFlower_UpdateCommand);
this.dtgFlower.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dtgFlower_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected System.Web.UI.WebControls.Label lblMessage;


SqlConnection sqlcon=new SqlConnection("server=(local);uid=sa;pwd=12345;database=Assignmen t");

public void BindGrid()
{
SqlDataAdapter sqlda=new SqlDataAdapter("select * from ProductFlower",sqlcon);
DataSet ds=new DataSet();
sqlda.Fill(ds,"ProductFlower");
dtgFlower.DataSource=ds.Tables["ProductFlower"].DefaultView;
dtgFlower.DataBind();
}

private void dtgFlower_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dtgFlower.Columns[0].HeaderText="Cancel";
dtgFlower.EditItemIndex=-1;
BindGrid();
}

//private TextBox txtDescribes,txtMoney;

private void dtgFlower_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

dtgFlower.Columns[5].HeaderText="Update";
int ID = Int32.Parse(dtgFlower.DataKeys[e.Item.ItemIndex].ToString());
string strUpdate="Update ProductFlower set Name=@Name,Describes=@Describes,Money=@Money where ProductID=ID";
SqlCommand sqlcom=new SqlCommand(strUpdate,sqlcon);

//sqlcom.Parameters.Add(new SqlParameter("@ProductID",SqlDbType.Int,4));
//sqlcom.Parameters.Add(new SqlParameter("@Image",SqlDbType.VarChar,100));
sqlcom.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar,100));
sqlcom.Parameters.Add(new SqlParameter("@Describes",SqlDbType.VarChar,100));
sqlcom.Parameters.Add(new SqlParameter("@Money",SqlDbType.Char,10));

//sqlcom.Parameters["@ProductID"].Value=dtgFlower.DataKeys[(int)e.Item.ItemIndex];
//sqlcom.Parameters["@Image"].Value=((TextBox)e.Item.Cells[2].Controls[0]).Text;
sqlcom.Parameters["@Name"].Value=((TextBox)e.Item.Cells[3].Controls[0]).Text;
sqlcom.Parameters["@Describes"].Value=(e.Item.FindControl("TextBox"));
sqlcom.Parameters["@Money"].Value=(e.Item.FindControl("TextBox"));
//sqlcom.Parameters["@Describes"].Value=((TextBox)e.Item.Cells[4].Controls[0]).Text;
//sqlcom.Parameters["@Money"].Value=((TextBox)e.Item.Cells[5].Controls[0]).Text;
sqlcon.Open();
try
{
sqlcom.ExecuteNonQuery();
lblMessage.Text="Completed Successful";
dtgFlower.EditItemIndex=-1;
}
catch(SqlException exc)
{
if(exc.Number==2627)
lblMessage.Text="Error";
else
lblMessage.Text=exc.ToString()+"Error:Cannot update";
}
sqlcon.Close();
BindGrid();
}

private void dtgFlower_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dtgFlower.Columns[5].HeaderText="Edit";
dtgFlower.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
}

private void dtgFlower_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dtgFlower.Columns[6].HeaderText="Delete";
string strDelete="Delete from ProductFlower where ProductID=@ProductID";
SqlCommand sqlcom=new SqlCommand(strDelete,sqlcon);
sqlcom.Parameters.Add(new SqlParameter("@ProductID",SqlDbType.Int,4));
sqlcom.Parameters["@ProductID"].Value=dtgFlower.DataKeys[(int)e.Item.ItemIndex];
sqlcon.Open();
try
{
sqlcom.ExecuteNonQuery();
lblMessage.Text="Completed Successful";
}
catch(SqlException)
{
lblMessage.Text="Error:Cannot delete";
}
sqlcon.Close();
BindGrid();
}

}
}

"

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] "

Recommended Answers

All 2 Replies

Pre-response disclaimer: I skimmed your code quickly to find what part of it was related to this query, and then took a guess at the problem.

In the lines

sqlcom.Parameters["@Name"].Value=((TextBox)e.Item.Cells[3].Controls[0]).Text;
sqlcom.Parameters["@Describes"].Value=(e.Item.FindControl("TextBox"));
sqlcom.Parameters["@Money"].Value=(e.Item.FindControl("TextBox"));

notice that for the first parameter you assign it a String (the .Text property of the control), whereas the other ones you are attempting to assign the control. You probably forgot to add the .Text or something like that.

Hope I got it right :icon_wink:

PS: in the future, please post your code inside [code] and [/code] tags, and please try to only post the relevant code segments. Thanks!

Also, can you post the code to your stored procedure? That might tell us even more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.