PDA

View Full Version : Specified cast is not valid



faramarz_s
جمعه 28 شهریور 1382, 16:50 عصر
در راستای رعایت قوانین...

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblPrimary]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tblPrimary]
GO

CREATE TABLE [dbo].[tblPrimary] (
[id] [int] NOT NULL ,
[un] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[uf] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[password] [nchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[email] [char] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

Vahid_Nasiri
جمعه 28 شهریور 1382, 19:00 عصر
تا اینجا ایرادی ندارد. بقیه اش چی بود؟ لطفا کوئری خود را هم بنویسید.

faramarz_s
جمعه 28 شهریور 1382, 19:34 عصر
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
string un = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
string uf = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
string password = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
string email = ((TextBox)e.Item.Cells[5].Controls[0]).Text;

SqlConnection conn =
new SqlConnection("server=(local);uid=sa;pwd=yaali;database=MDB;");

// Prepare the command text
String strCmd = "UPDATE tblPrimary SET password='"+
password+ "'" + "email='" + email+ "'"+
" WHERE id=" + id;


SqlCommand cmd = new SqlCommand(strCmd, conn);

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

// Reset the edit mode for the current item
DataGrid1.EditItemIndex = -1;

// create data source again.........
bindToDatGrid("id");
}

Vahid_Nasiri
جمعه 28 شهریور 1382, 19:51 عصر
شما به این کوئری دقت کنید:


String strCmd = "UPDATE tblPrimary SET password='"+
password+ "'" + "email='" + email+ "'"+
" WHERE id=" + id;

آیا ویرگولی چیزی جا نیانداخته اید؟

faramarz_s
جمعه 28 شهریور 1382, 21:41 عصر
String strCmd = "UPDATE tblPrimary SET un='"+
un+ "'," + "uf='" + uf+ "'"+
" WHERE id='" + id+"'";


Specified cast is not valid.
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.InvalidCastException: Specified cast is not valid.

Source Error:


Line 82: private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
Line 83: {
Line 84: string id = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
Line 85: string un = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
Line 86: string uf = ((TextBox)e.Item.Cells[3].Controls[0]).Text;

faramarz_s
شنبه 29 شهریور 1382, 09:33 صبح
آیا باز هم مشکلی هست،چرا همان پیغام را می دهد؟

Vahid_Nasiri
شنبه 29 شهریور 1382, 14:29 عصر
شما از try و catch‌ استفاده کن . سپس در قسمت catch‌ یک لیبل را که برای دیباگ روی صفحه گذاشته ای و خواهی گذاشت را با مقادیر uf و غیره پر کن و نمایش بده. اون وقت معلوم میشه که آیا اصلا چیزی را بر می گرداند یا خیر.

faramarz_s
شنبه 29 شهریور 1382, 20:57 عصر
ظاهرا مشکل از جای دیگری است!!!!
کلا من همه آنچه داخل آپدیت بود را غیر فعال کردم و باز هم همان پیغام :oops:

Specified cast is not valid.
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.InvalidCastException: Specified cast is not valid.

Source Error:


Line 130:
Line 131: private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
Line 132: {
Line 133: Label2.Text="It is Funny!";
Line 134: /*