Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 19th, 2004, 11:11 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error when using DataGrid edit mode??


Hey guys I have a datagrid that displays the data just fine. However, when I click edit, change a value and hit update I get this error:

-------------
Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 87:
Line 88: MyConn.Open();
Line 89: myCommand.ExecuteNonQuery();
Line 90: MyConn.Close();
Line 91:
-------------

Here is my code:

Code:
private string sMyConn = "server=\'DC1\'; trusted_connection=true; database=\'presents\'";

void Page_Load(Object sender, EventArgs e)
{

    getPresents();

}


public void getPresents()
{

    SqlConnection MyConn = new SqlConnection(sMyConn);


    StringBuilder sb = new StringBuilder();
    sb.Append("select * ");
    sb.Append("from my_presents");

    SqlCommand myCommand = new SqlCommand(sb.ToString(), MyConn);

    MyConn.Open();
    myDataGrid.DataSource = myCommand.ExecuteReader();
    myDataGrid.DataBind();
    MyConn.Close();


}

public void myDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
    myDataGrid.EditItemIndex = e.Item.ItemIndex;
    getPresents();
}

public void myDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
    myDataGrid.EditItemIndex = -1;
    getPresents();
}

public void myDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
    string sName, sUrl, sPrice, iPresentID;


    sName = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
    sUrl = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
    sPrice = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
    iPresentID = e.Item.Cells[5].Text;

    StringBuilder sb = new StringBuilder();
    sb.Append("Update my_presents ");
    sb.Append("set present_name = @present_name,");
    sb.Append("present_url = @present_url,");
    sb.Append("present_price = @present_price");
    sb.Append("where presentID = @presentID");

    SqlConnection MyConn = new SqlConnection(sMyConn);

    SqlCommand myCommand = new SqlCommand(sb.ToString(), MyConn);

    myCommand.Parameters.Add(new SqlParameter("@present_name", SqlDbType.VarChar, 200));
    myCommand.Parameters["@present_name"].Value = sName;

    myCommand.Parameters.Add(new SqlParameter("@present_url", SqlDbType.VarChar, 500));
    myCommand.Parameters["@present_url"].Value = sUrl;

    myCommand.Parameters.Add(new SqlParameter("@present_price", SqlDbType.SmallInt));
    myCommand.Parameters["@present_price"].Value = sPrice;


    myCommand.Parameters.Add(new SqlParameter("@presentID", SqlDbType.SmallInt));
    myCommand.Parameters["@presentID"].Value = iPresentID;

    MyConn.Open();
    myCommand.ExecuteNonQuery();
    MyConn.Close();

    myDataGrid.EditItemIndex = -1;
    getPresents();


}
Any help would be greatly appreciated.

Take care,

Chris

 
Old November 22nd, 2004, 09:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

sb.Append("present_price = @present_price");
sb.Append("where presentID = @presentID");

Try adding a space before the 'WHERE'.

 
Old November 22nd, 2004, 07:00 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Thanks but I tried not and I recieved the same error.

Thanks,

Chris





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation in Edit mode of Datagrid Control pramesh_it ASP.NET 1.0 and 1.1 Basics 1 April 3rd, 2006 10:29 AM
Edit mode I do not see the ddlist datagrid macupryk General .NET 0 October 10th, 2004 09:23 PM
Datagrid - edit mode Programator ASP.NET 1.0 and 1.1 Professional 1 September 5th, 2003 08:38 AM
Datagrid - edit mode Programator ASP.NET 1.x and 2.0 Application Design 1 September 5th, 2003 08:37 AM
Datagrid - edit mode Programator Classic ASP Professional 0 September 5th, 2003 03:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.