dataRow在datatable c#中出现上下文错误

时间:2015-11-20 05:56:59

标签: c# datatable

下面是我的代码,我发现workRow变量的上下文错误,请解决此问题我尝试了所有内容,例如编写单独的函数来添加行。 。这对我没有帮助。 。只有(reader.Name == "Result")我想创建roe,而在其他情况下如果想在同一行中添加列

while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element:
           if (reader.Name == "Result")
           {
              DataRow workRow = dt.NewRow();
           }               
           if (columns.Contains(reader.Name))
           {
              //ERROR IS HERE out of context
              workRow[reader.Name] = reader.Value;
           }
           writer.WriteStartElement(reader.Name);
           break;
       case XmlNodeType.Text:
           writer.WriteString(reader.Value);
           break;
       case XmlNodeType.XmlDeclaration:
       case XmlNodeType.ProcessingInstruction:  
           writer.WriteProcessingInstruction(reader.Name, reader.Value);
           break;
       case XmlNodeType.Comment:
           writer.WriteComment(reader.Value);
           break;
       case XmlNodeType.EndElement:
           writer.WriteFullEndElement();
           break;
    }
}

3 个答案:

答案 0 :(得分:3)

你的workRow变量是在另一个if语句中声明的,这就是你得到错误的原因。

DataRow workRow; // Moved the declaration here
while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element:
            if (reader.Name == "Result")
            {
                workRow = dt.NewRow(); // this is okay if Result always comes first
            }

            if (columns.Contains(reader.Name))
            {
                //ERROR IS HERE out of context
                workRow[reader.Name] = reader.Value;
            }
            writer.WriteStartElement(reader.Name);
            break;
        case XmlNodeType.Text:
            writer.WriteString(reader.Value);
            break;
        case XmlNodeType.XmlDeclaration:
        case XmlNodeType.ProcessingInstruction:
            writer.WriteProcessingInstruction(reader.Name, reader.Value);
            break;
        case XmlNodeType.Comment:
            writer.WriteComment(reader.Value);
            break;
        case XmlNodeType.EndElement:
            writer.WriteFullEndElement();
            break;
    }
}

答案 1 :(得分:2)

如果如下所示,请先在workRow外面声明,或者先将 LatLng currentAddressLatLong; currentAddressLatLong = new LatLng(gps.getLatitude(), gps.getLongitude()); RippleBackground rippleLayout=(RippleBackground)findViewById(R.id.rippleLayout); rippleLayout.setVisibility(View.VISIBLE); ImageView imgMapProfile=(ImageView)findViewById(R.id.imgMapProfile); Bitmap markerUserBitmap = Comman.createDrawableFromView(NearByActivity.this, rippleLayout); googleMap.addMarker(new MarkerOptions().position(currentAddressLatLong).icon(BitmapDescriptorFactory.fromBitmap(markerUserBitmap))); googleMap.moveCamera(CameraUpdateFactory.newLatLng(currentAddressLatLong)); googleMap.animateCamera(CameraUpdateFactory.zoomTo(11)); rippleLayout.startRippleAnimation(); // Convert a view to bitmap function public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } 添加到内部,如下所示。问题是你在workRow[reader.Name]范围内确定你的workRow变量,所以当你试图在这个if块之外访问workRow时你会得到错误。请检查可能适合您的样本修改

if (reader.Name == "Result")

答案 2 :(得分:1)

工作流程声明为正确的if语句内部,如下所示。

if (reader.Name == "Result")
   {
       //DataRow workRow = dt.NewRow();
   }
if (columns.Contains(reader.Name))
   {
       DataRow workRow = dt.NewRow();
       //ERROR IS HERE out of context
       workRow[reader.Name] = reader.Value;
   }