الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

a data view problem

مغلق
بدأه Miss_Dev في 29 أبريل 2005 · 6 رد · 648 مشاهدة · في ASP.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
Function GetProducts() As DataView
  Dim dstProducts As DataSet
  Dim conNorthwind As SqlConnection
  Dim dadProducts As SqlDataAdapter
  Dim dvwProducts As DataView

  dvwProducts = Cache( "Products" )
  If dvwProducts Is Nothing Then
    dstProducts = New DataSet()
   conNorthwind = New SqlConnection(
"Server=localhost;UID=sa;PWD=secret;Database=Northwind" )
    dadProducts = New SqlDataAdapter( "Select * from Products", conNorthwind )
    dadProducts.Fill( dstProducts, "Products" )
    dvwProducts = dstProducts.Tables( "Products" ).DefaultView()
    Cache( "Products" ) = dvwProducts
  End If
  Return dvwProducts
End Function

i got an error at the line inside which i put the dvwProducts inside the cache it says he cannot convert from datarow view to dataview

i tried many castings but no way and i copied the code form asp.net unleashed

any help????

موقع سلطان للروابط المجمعة

طريق الاسلام

الاسلام سؤال و جواب

****من قال فى اليوم سبحان الله و بحمده مائه مرة غفرت ذنوبه و أن كانت كزبد البحر****

Das Alter des Schlafens ist gegangen

من لاح له فجر الاجر ... هان عليه ظلام التكليف

زوروا أول موقع لى

#2

السلام عليكم

الخطأ غالباً ليس في هذا الجزء لأنه سليم برمجياً ، والخطأ يذكر DataRow ولا يوجد ذكر لأيDataRow هنا فأين جزء الكود الموجود به DataRow ؟

#3

انا لا اعرف الحقيقة ما مشكلته و انا جربت اكتب نفس الكود بالسى شارب كالتالى

public DataView GetProducts()
  {
 	 DataSet dsProducts;
 	 DataView dvProducts;
 	 dvProducts=Cache["Products"];

 	 if(dvProducts ==null)
 	 { 
    dvProducts=(System.Data.DataView)dsProducts.Tables["Products"].DefaultView[0];
    Cache ["Products"]=dvProducts;
 	 
 	 }
 	 return dvProducts;
  }

و نفس السطر خطا يقول

C:\Inetpub\wwwroot\GridDemo\democache.aspx.cs(42): Cannot implicitly convert type 'object' to 'System.Data.DataView'

!!!!

موقع سلطان للروابط المجمعة

طريق الاسلام

الاسلام سؤال و جواب

****من قال فى اليوم سبحان الله و بحمده مائه مرة غفرت ذنوبه و أن كانت كزبد البحر****

Das Alter des Schlafens ist gegangen

من لاح له فجر الاجر ... هان عليه ظلام التكليف

زوروا أول موقع لى

#4

كما ذكرت لك المشكلة ليست هنا بل في جزء آخر

ربما لو ذكرت بقية الكود المرتبط بهذا الكود المذكور ربما أمكننا المساعدة

وأتوقع أن يكون الخطأ مثل هذا

dim dr as datarow = GetProducts()
#5

this is all the code in the page :(

[code]private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

this.DataGrid1.DataSource=GetProducts();

this.DataGrid1.DataBind();

}

}

public DataView GetProducts()

{

DataSet dsProducts;

DataView dvProducts;

//dvProducts=Cache["Products"];

if(dvProducts ==null)

{

//dvProducts=(System.Data.DataView)dsProducts.Tables["Products"].DefaultView[0];

Cache ["Products"]=dvProducts ;

}

return dvProducts ;

}

#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.sqlConnection1 = new System.Data.SqlClient.SqlConnection();

this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();

this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();

this.dsProducts1 = new GridDemo.dsProducts();

((System.ComponentModel.ISupportInitialize)(this.dsProducts1)).BeginInit();

this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);

//

// sqlConnection1

//

this.sqlConnection1.ConnectionString = "workstation id=NANA;packet size=4096;integrated security=SSPI;data source=\".\";per" +

"sist security info=False;initial catalog=Northwind";

//

// sqlDataAdapter1

//

this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;

this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;

this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;

this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {

new System.Data.Common.DataTableMapping("Table", "Products", new System.Data.Common.DataColumnMapping[] {

new System.Data.Common.DataColumnMapping("ProductID", "ProductID"),

new System.Data.Common.DataColumnMapping("ProductName", "ProductName"),

new System.Data.Common.DataColumnMapping("SupplierID", "SupplierID"),

new System.Data.Common.DataColumnMapping("CategoryID", "CategoryID"),

new System.Data.Common.DataColumnMapping("QuantityPerUnit", "QuantityPerUnit"),

new System.Data.Common.DataColumnMapping("UnitPrice", "UnitPrice"),

new System.Data.Common.DataColumnMapping("UnitsInStock", "UnitsInStock"),

new System.Data.Common.DataColumnMapping("UnitsOnOrder", "UnitsOnOrder"),

new System.Data.Common.DataColumnMapping("ReorderLevel", "ReorderLevel"),

new System.Data.Common.DataColumnMapping("Discontinued", "Discontinued")})});

this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;

//

// sqlSelectCommand1

//

this.sqlSelectCommand1.CommandText = "SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice" +

", UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products";

this.sqlSelectCommand1.Connection = this.sqlConnection1;

//

// sqlInsertCommand1

//

this.sqlInsertCommand1.CommandText = @"INSERT INTO Products(ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued) VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued); SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products WHERE (ProductID = @@IDENTITY)";

this.sqlInsertCommand1.Connection = this.sqlConnection1;

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProductName", System.Data.SqlDbType.NVarChar, 40, "ProductName"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SupplierID", System.Data.SqlDbType.Int, 4, "SupplierID"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryID", System.Data.SqlDbType.Int, 4, "CategoryID"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@QuantityPerUnit", System.Data.SqlDbType.NVarChar, 20, "QuantityPerUnit"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UnitPrice", System.Data.SqlDbType.Money, 8, "UnitPrice"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UnitsInStock", System.Data.SqlDbType.SmallInt, 2, "UnitsInStock"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UnitsOnOrder", System.Data.SqlDbType.SmallInt, 2, "UnitsOnOrder"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ReorderLevel", System.Data.SqlDbType.SmallInt, 2, "ReorderLevel"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Discontinued", System.Data.SqlDbType.Bit, 1, "Discontinued"));

//

// sqlUpdateCommand1

//

this.sqlUpdateCommand1.CommandText = @"UPDATE Products SET ProductName = @ProductName, SupplierID = @SupplierID, CategoryID = @CategoryID, QuantityPerUnit = @QuantityPerUnit, UnitPrice = @UnitPrice, UnitsInStock = @UnitsInStock, UnitsOnOrder = @UnitsOnOrder, ReorderLevel = @ReorderLevel, Discontinued = @Discontinued WHERE (ProductID = @Original_ProductID) AND (CategoryID = @Original_CategoryID OR @Original_CategoryID IS NULL AND CategoryID IS NULL) AND (Discontinued = @Original_Discontinued) AND (ProductName = @Original_ProductName) AND (QuantityPerUnit = @Original_QuantityPerUnit OR @Original_QuantityPerUnit IS NULL AND QuantityPerUnit IS NULL) AND (ReorderLevel = @Original_ReorderLevel OR @Original_ReorderLevel IS NULL AND ReorderLevel IS NULL) AND (SupplierID = @Original_SupplierID OR @Original_SupplierID IS NULL AND SupplierID IS NULL) AND (UnitPrice = @Original_UnitPrice OR @Original_UnitPrice IS NULL AND UnitPrice IS NULL) AND (UnitsInStock = @Original_UnitsInStock OR @Original_UnitsInStock IS NULL AND UnitsInStock IS NULL) AND (UnitsOnOrder = @Original_UnitsOnOrder OR @Original_UnitsOnOrder IS NULL AND UnitsOnOrder IS NULL); SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products WHERE (ProductID = @ProductID)";

this.sqlUpdateCommand1.Connection = this.sqlConnection1;

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProductName", System.Data.SqlDbType.NVarChar, 40, "ProductName"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SupplierID", System.Data.SqlDbType.Int, 4, "SupplierID"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryID", System.Data.SqlDbType.Int, 4, "CategoryID"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@QuantityPerUnit", System.Data.SqlDbType.NVarChar, 20, "QuantityPerUnit"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UnitPrice", System.Data.SqlDbType.Money, 8, "UnitPrice"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UnitsInStock", System.Data.SqlDbType.SmallInt, 2, "UnitsInStock"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UnitsOnOrder", System.Data.SqlDbType.SmallInt, 2, "UnitsOnOrder"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ReorderLevel", System.Data.SqlDbType.SmallInt, 2, "ReorderLevel"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Discontinued", System.Data.SqlDbType.Bit, 1, "Discontinued"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ProductID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ProductID", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Discontinued", System.Data.SqlDbType.Bit, 1, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Discontinued", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ProductName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ProductName", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_QuantityPerUnit", System.Data.SqlDbType.NVarChar, 20, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "QuantityPerUnit", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ReorderLevel", System.Data.SqlDbType.SmallInt, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ReorderLevel", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_SupplierID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SupplierID", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_UnitPrice", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "UnitPrice", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_UnitsInStock", System.Data.SqlDbType.SmallInt, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "UnitsInStock", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_UnitsOnOrder", System.Data.SqlDbType.SmallInt, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "UnitsOnOrder", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProductID", System.Data.SqlDbType.Int, 4, "ProductID"));

//

// sqlDeleteCommand1

//

this.sqlDeleteCommand1.CommandText = @"DELETE FROM Products WHERE (ProductID = @Original_ProductID) AND (CategoryID = @Original_CategoryID OR @Original_CategoryID IS NULL AND CategoryID IS NULL) AND (Discontinued = @Original_Discontinued) AND (ProductName = @Original_ProductName) AND (QuantityPerUnit = @Original_QuantityPerUnit OR @Original_QuantityPerUnit IS NULL AND QuantityPerUnit IS NULL) AND (ReorderLevel = @Original_ReorderLevel OR @Original_ReorderLevel IS NULL AND ReorderLevel IS NULL) AND (SupplierID = @Original_SupplierID OR @Original_SupplierID IS NULL AND SupplierID IS NULL) AND (UnitPrice = @Original_UnitPrice OR @Original_UnitPrice IS NULL AND UnitPrice IS NULL) AND (UnitsInStock = @Original_UnitsInStock OR @Original_UnitsInStock IS NULL AND UnitsInStock IS NULL) AND (UnitsOnOrder = @Original_UnitsOnOrder OR @Original_UnitsOnOrder IS NULL AND UnitsOnOrder IS NULL)";

this.sqlDeleteCommand1.Connection = this.sqlConnection1;

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ProductID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ProductID", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Discontinued", System.Data.SqlDbType.Bit, 1, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Discontinued", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ProductName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ProductName", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_QuantityPerUnit", System.Data.SqlDbType.NVarChar, 20, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "QuantityPerUnit", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ReorderLevel", System.Data.SqlDbType.SmallInt, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ReorderLevel", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_SupplierID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SupplierID", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_UnitPrice", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "UnitPrice", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_UnitsInStock", System.Data.SqlDbType.SmallInt, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "UnitsInStock", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_UnitsOnOrder", System.Data.SqlDbType.SmallInt, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "UnitsOnOrder", System.Data.DataRowVersion.Original, null));

//

// dsProducts1

//

this.dsProducts1.DataSetName = "dsProducts";

this.dsProducts1.Locale = new System.Globalization.CultureInfo("ar-EG");

this.Load += new System.EventHandler(this.Page_Load);

((System.ComponentModel.ISupportInitialize)(this.dsProducts1)).EndInit();

}

#endregion

private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

{

DataView dvProducts;

dvProducts=GetProducts();

dvProducts.Sort=e.SortExpression ;

this.DataGrid1.DataSource=dvProducts ;

this.DataGrid1.DataBind();

}

}

}

[code/]

موقع سلطان للروابط المجمعة

طريق الاسلام

الاسلام سؤال و جواب

****من قال فى اليوم سبحان الله و بحمده مائه مرة غفرت ذنوبه و أن كانت كزبد البحر****

Das Alter des Schlafens ist gegangen

من لاح له فجر الاجر ... هان عليه ظلام التكليف

زوروا أول موقع لى

#6

بالنسبة للخطأ الثاني فهو خاص بعملية التحويل، لان السي شارب لا تقوم بعملية تحويل ضمني implicit casting مثل الفي بي

غيري السطر الى

dvProducts=(DataView)Cache["Products"];

في السؤال الاول

هي يظهر الخطأ في هذا السطر

 dvwProducts = Cache( "Products" )

أم أين؟، إذا كان هو السطر السابق، فماذا تم تخزين داخل الكاش تحت إسم Products قبل إستدعاء هذه الدالة؟

لا تحزن:

إن كنت فقيرا فغيرك محبوس في دين، وإن كنت لا تملك وسيلة نقل فسواك مبتور القدمين، وان كنت تشكوا من آلام فغيرك يرقدون على الاسرة البيضاء و من سنوات، وان فقدت ولدا فغيرك فقد عددا من الأولاد و في حادث واحد.

لا تحزن:

فأنت تشرب الماء الزلال، و تستنشق الهواء الطلق، و تمشي على قدميك معافى، و تنام ليلك آمنا.

#7

لم أنتبه للخطأ الثاني فأنا لم أقرأ من الكلام سوى

"انا لا اعرف الحقيقة ما مشكلته و انا جربت اكتب نفس الكود بالسى شارب كالتالي"

فظننت أنها نفس المشكلة ، وعلى هذا الأساس كان ردي الثاني ، وطبعا المشكلة الثانية مشكلة ظهرت أثناء التحويل من VB إلى C# ، ولقد ذكر الحل أخي محمد فجزاه الله خيراً

أما المشكلة الأساسية فأضع لك كود منقح وإن شاء الله يعمل ، وللفائدة يرجى مقارنته بالكود المذكور

  public DataView GetProducts()
  {
 	 DataSet dsProducts;
 	 DataView dvProducts;
 	 dvProducts=(DataView)Cache["Products"];
            dsProducts=new DataSet();
 	 if(dvProducts ==null)
 	 {
    sqlDataAdapter1.Fill( dsProducts, "Products" );
    dvProducts=(DataView)dsProducts.Tables["Products"].DefaultView;
    Cache ["Products"]=dvProducts;

 	 }
 	 return dvProducts;
  }

هذا بفرض أن sqlDataAdapter1 معرف للجدول Products

هذا الموضوع مغلق.

مواضيع مشابهة