Add Object To Other Object Scripts that require an Object - unity3d

Ok , I have problem with adding object let's first see what is this about.
I Have Map when i click at map i create Sphere on map.
then i serialized the point, msg and name .....
void Start () {
Ser = new Serial();
Ser.read();
for (int i =0 ; i < Ser.list.Count; i++)
{
Data d = new Data();
d = Ser.list[i];
GameObject a = GameObject.CreatePrimitive(PrimitiveType.Sphere);
a.name = d.Id;
a.transform.position = new Vector3(d.x,d.y,0);
a.transform.localScale =new Vector3(0.5f , 0.5f , 0.5f);
Follow putTarget = new Follow();
Data Pos_In_File = new Data();
Pos_In_File = Ser.list[i];
GUIText GText = new GUIText();
Vector3 SP = new Vector3();
GameObject TextObj = new GameObject("GUIText" + Convert.ToString(i));
GText = (GUIText)TextObj.AddComponent(typeof(GUIText));
GText.text = Pos_In_File.msg;
GText.material.color = new Color(0 , 0 , 1);
GText.fontSize = 20;
GText.fontStyle = FontStyle.Bold;
SP = Camera.main.WorldToViewportPoint(new Vector3(Pos_In_File.x , Pos_In_File.y , Pos_In_File.z));
TextObj.transform.position = SP;
TextObj.AddComponent(typeof(Follow));
GameObject OBJ = GameObject.Find(Pos_In_File.Id);
putTarget =GetComponent<Follow>();
putTarget.target = OBJ.transform;
}
}
now in last three lines i want to add OBJ to TextOBJ->Follow(Script)->target
What i am messing up ?

i asked this quastion long ago my software have been finnished a 10 month
and the i get through this problem is
void Start () {
Resources.UnloadUnusedAssets();
Ser = new Serial();
Screen.fullScreen = true;
Ser.read();
for (int i =0 ; i < Ser.list.Count; i++)
{
Data d = new Data();
d = Ser.list[i];
GameObject a = GameObject.CreatePrimitive(PrimitiveType.Sphere);
a.name = d.Id;
a.transform.position = new Vector3(d.x,d.y,0);
a.renderer.material.color = Color.green;
Data Pos_In_File = new Data();
Pos_In_File = Ser.list[i];
GUIText GText = new GUIText();
Vector3 SP = new Vector3();
GameObject TextObj = new GameObject(Pos_In_File.Id+"h");
GText = (GUIText)TextObj.AddComponent(typeof(GUIText));
GText.text = Pos_In_File.msg;
GText.material.color = new Color(0 , 0 , 1);
GText.fontSize = 20 + (int)Camera.main.transform.position.z;
GText.fontStyle = FontStyle.Bold;
SP = Camera.main.WorldToViewportPoint(new Vector3(Pos_In_File.x , Pos_In_File.y , Pos_In_File.z));
TextObj.transform.position = SP;
TextObj.AddComponent(typeof(Follow));
}

Related

Copying Mesh effectively

I've needed to copy a mesh with submeshes in case there are more than one material. I've came up with this code.
public static Mesh Copy(this Mesh original)
{
Mesh m = new Mesh();
original.CopyTo(ref m);
return m;
}
public static void CopyTo(this Mesh original, ref Mesh copy)
{
copy.name = original.name;
copy.vertices = original.vertices;
copy.normals = original.normals;
copy.tangents = original.tangents;
copy.triangles = original.triangles;
copy.bounds = original.bounds;
copy.uv = original.uv;
copy.uv2 = original.uv2;
copy.uv3 = original.uv3;
copy.uv4 = original.uv4;
copy.uv5 = original.uv5;
copy.uv6 = original.uv6;
copy.uv7 = original.uv7;
copy.uv8 = original.uv8;
copy.colors = original.colors;
copy.bindposes = original.bindposes;
copy.boneWeights = original.boneWeights;
copy.indexFormat = original.indexFormat;
copy.indexBufferTarget = original.indexBufferTarget;
copy.vertexBufferTarget = original.vertexBufferTarget;
copy.subMeshCount = original.subMeshCount;
for (int i = 0; i < original.subMeshCount; i++)
{
copy.SetSubMesh(i, original.GetSubMesh(i));
}
}
Do I miss something?
Also does it save any memory if I use CopyTo to reuse existing mesh?

How to NOT flatten the image applied in a pdf with Itext in c#?

I need to apply an image on all my pages from my PDF, but without flattening it ( I want to have the ability to move it in my PDF reader afterwards)
My code:
String basePath = "d:\\zPDF\\";
DirectoryInfo d = new DirectoryInfo(basePath);//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.pdf"); //Getting Text files
List<string> listS = new List<string>();
foreach (FileInfo file in Files)
{
listS.Add(file.Name);
}
foreach (string s in listS)
{
using (System.IO.Stream inputPdfStream = new FileStream(basePath + s, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
//using (System.IO.Stream inputImageStream = new FileStream(basePath + "x1.wmf", FileMode.Open, System.IO.FileAccess.Read, FileShare.Read))
using (System.IO.Stream inputImageStream2 = new FileStream(basePath + "x2.wmf", FileMode.Open, System.IO.FileAccess.Read, FileShare.Read))
using (System.IO.Stream outputPdfStream = new FileStream(basePath + "zResult" + s, FileMode.Create, System.IO.FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
//stamper.FormFlattening = true;
//stamper.FreeTextFlattening = true;
int numberOfPages = reader.NumberOfPages;
Image myImage = Image.GetInstance(inputImageStream2);
float f-Image1, f-Image2;
for (int i = 1; i <= numberOfPages; i++)
{
int nr, plusMinus = 25;
Rectangle mediabox = reader.GetPageSize(i);
int getTOP = (int)mediabox.GetTop(0);
int getRight = (int)mediabox.GetRight(0);
var pdfContentByte = stamper.GetOverContent(i);
nr = getRight - 600;
f-Image1 = row1(nr, nr + plusMinus);
//row1 - generates a random number between those 2 values
nr = 40;
f-Image2 = row1(nr, nr + plusMinus);
//row1 - generates a random number between those 2 values
myImage.SetAbsolutePosition(f-Image1, f-Image2);
myImage.RotationDegrees = row1(-35, 35);
pdfContentByte.AddImage(myImage);
}
stamper.Close();
}
I've tried:
stamper.FormFlattening = false;
stamper.FreeTextFlattening = false;
but no results. The image is still flatten.
I think, after I've read some posts, that I need to set up my PdfStamper in useAppendMode() but I don't know how should I do this and, I don't know if this is this right direction.

Hundred thousands of datatable records to PDF in web API

I'm trying to create PDF from the DataTable in web api using ADO.Net. Unfortunately based on filters some times I may get very less records & able to download without any problem. Sometimes may be very huge like 200 thousand of records. When I'm checking in local my system its getting hang while converting the dt to PDF. My code is like below:
private FileContentResult ExportPDF(DataTable dataTable)
{
string Name = "Logs";
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
byte[] content = null;
try
{
string[] columnNames = (from dc in dataTable.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();
int count = columnNames.Length;
object[] array = new object[count];
dataTable.Rows.Add(array);
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
int cols = dataTable.Columns.Count;
int rows = dataTable.Rows.Count;
HeaderFooter header = new HeaderFooter(new Phrase(Name), false);
// Remove the border that is set by default
header.Border = iTextSharp.text.Rectangle.TITLE;
// Align the text: 0 is left, 1 center and 2 right.
header.Alignment = Element.ALIGN_CENTER;
pdfDoc.Header = header;
// Header.
pdfDoc.Open();
iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
pdfTable.BorderWidth = 1; pdfTable.Width = 100;
pdfTable.Padding = 1; pdfTable.Spacing = 4;
//creating table headers
for (int i = 0; i < cols; i++)
{
Cell cellCols = new Cell();
Chunk chunkCols = new Chunk();
iTextSharp.text.Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.Black);
chunkCols = new Chunk(dataTable.Columns[i].ColumnName, ColFont);
cellCols.Add(chunkCols);
pdfTable.AddCell(cellCols);
}
//creating table data (actual result)
for (int k = 0; k < rows; k++)
{
for (int j = 0; j < cols; j++)
{
Cell cellRows = new Cell();
iTextSharp.text.Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}
pdfDoc.Add(pdfTable);
pdfDoc.Close();
content = mStream.ToArray();
return File(content, "application/pdf", "LogReports.pdf");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

Unity: procedural generated mesh vertices's position are wrong/off

I'm generating points and mesh on run-time, the problem is, the generated mesh vertices appeared in the wrong position, even if I pass on the point's global position.
the mesh generating part is made in MakeMeshData();
I tired calculating with all my points with position instead of localPosition, as well as commenting out the parenting, still the same result .
https://imgur.com/a/YAlpXfh
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPG03_Corridor : MonoBehaviour
{
public Transform centerPoint;
public Transform startPoint;
public Transform endPoint;
public enum CORRIDORTYPE
{
LinearBezierCurves,
QuadraticBezierCurves,
CubicBezierCurve
}
public CORRIDORTYPE corridorType;
public List<Transform> centerLinePoints = new List<Transform>();
public List<Transform> corridorAllPoint = new List<Transform>();
List<Transform> rightPoints = new List<Transform>();
List<Transform> leftPoints = new List<Transform>();
public List<GameObject> listOfCorridorFloor = new List<GameObject>();
public Vector3[] corridorVertices;
public int[] corridorTrangle;
public float StartEndDistanceFromCenter = 15;
public float minStartEndDistanceFromCenter = 10;
public float maxStartEndDistanceFromCenter = 20;
public int controlDirection = 1;
public float minControlDistanceFromCenter = 10;
public float maxControlDistanceFromCenter = 30;
public int numberOfLinePoint = 15;
public int numberOfMesh;
public Vector3[] testVec = new Vector3[3];
int[] textInt = new int[3];
private void Start()
{
BasiceSetup();
PointsSetups();
MakeMeshData();
}
private void BasiceSetup()
{
numberOfMesh = (numberOfLinePoint - 1);
GameObject pointHolderObject = new GameObject();
GameObject centerPointObject = new GameObject();
GameObject startPointObject = new GameObject();
GameObject endPostionObject = new GameObject();
centerPoint = centerPointObject.transform;
startPoint = startPointObject.transform;
endPoint = endPostionObject.transform;
pointHolderObject.name = "Point Holder";
centerPointObject.name = "Center Point";
startPointObject.name = "Start Point";
endPostionObject.name = "End Point";
pointHolderObject.transform.parent = transform;
centerPointObject.transform.parent = pointHolderObject.transform;
startPointObject.transform.parent = pointHolderObject.transform;
endPostionObject.transform.parent = pointHolderObject.transform;
pointHolderObject.transform.localPosition = Vector3.zero;
pointHolderObject.transform.localRotation = Quaternion.Euler(Vector3.zero);
centerPointObject.transform.localPosition = Vector3.zero;
centerPointObject.transform.localRotation = Quaternion.Euler(Vector3.zero);
startPointObject.transform.localPosition = Vector3.zero;
startPointObject.transform.localRotation = Quaternion.Euler(new Vector3(0, 180, 0));
endPostionObject.transform.localPosition = Vector3.zero;
endPostionObject.transform.localRotation = Quaternion.Euler(Vector3.zero);
Vector3 newStartExitPostion = Vector3.forward * (Random.Range(minStartEndDistanceFromCenter, maxStartEndDistanceFromCenter));
startPointObject.transform.localPosition = -1 * newStartExitPostion;
endPostionObject.transform.localPosition = newStartExitPostion;
NPG01_ModulePoint centerPointScript = centerPointObject.AddComponent<NPG01_ModulePoint>();
NPG01_ModulePoint startPointScript = startPointObject.AddComponent<NPG01_ModulePoint>();
NPG01_ModulePoint endPointScript = endPostionObject.AddComponent<NPG01_ModulePoint>();
centerPointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.CENTER);
startPointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.CONNECTION);
endPointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.CONNECTION);
}
private void PointsSetups()
{
GameObject curveHolder = new GameObject();
curveHolder.transform.parent = transform;
curveHolder.name = "Curve Holder";
curveHolder.transform.localPosition = Vector3.zero;
curveHolder.transform.localRotation = Quaternion.Euler(Vector3.zero);
switch (corridorType)
{
case CORRIDORTYPE.LinearBezierCurves:
for (int i = 0; i < numberOfLinePoint; i++)
{
GameObject point = new GameObject();
point.name = "Curve Point " + i.ToString();
point.transform.parent = curveHolder.transform;
point.transform.localPosition = Vector3.zero;
point.transform.localRotation = Quaternion.Euler(Vector3.zero);
NPG01_ModulePoint pointScript = point.AddComponent<NPG01_ModulePoint>();
pointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.BEZIERCURVES);
centerLinePoints.Add(point.transform);
}
for (int i = 0; i < numberOfLinePoint; i++)
{
float t = (float)i / ((float)numberOfLinePoint - 1);
centerLinePoints[i].localPosition = LinearBezierCurves(startPoint.localPosition, endPoint.localPosition, t);
GameObject rightPoint = new GameObject();
GameObject leftPoint = new GameObject();
rightPoint.transform.parent = curveHolder.transform;
rightPoint.name = "Right " + i;
rightPoint.transform.localRotation = Quaternion.Euler(Vector3.zero);
rightPoint.transform.localPosition = centerLinePoints[i].localPosition + (Vector3.right * 3);
NPG01_ModulePoint rightPointScript = rightPoint.AddComponent<NPG01_ModulePoint>();
rightPointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.BEZIERCURVES);
leftPoint.transform.parent = curveHolder.transform;
leftPoint.name = "Left " + i;
leftPoint.transform.localRotation = Quaternion.Euler(Vector3.zero);
leftPoint.transform.localPosition = centerLinePoints[i].localPosition + (-Vector3.right * 3);
NPG01_ModulePoint leftPointScript = leftPoint.AddComponent<NPG01_ModulePoint>();
leftPointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.BEZIERCURVES);
rightPoints.Add(rightPoint.transform);
leftPoints.Add(leftPoint.transform);
}
for (int i = 0; i < numberOfLinePoint; i++)
{
corridorAllPoint.Add(centerLinePoints[i]);
corridorAllPoint.Add(leftPoints[i]);
corridorAllPoint.Add(rightPoints[i]);
}
break;
case CORRIDORTYPE.QuadraticBezierCurves:
int controlPointDirction = Random.Range(0, 9);
if (controlPointDirction % 2 == 0)
{
controlDirection = 1;
}
else
{
controlDirection = -1;
}
GameObject controlPointObject = new GameObject();
controlPointObject.name = "Control Point";
controlPointObject.transform.parent = curveHolder.transform;
controlPointObject.transform.localPosition = Vector3.right * (20 * controlDirection);
controlPointObject.transform.localPosition = new Vector3(controlPointObject.transform.localPosition.x,
controlPointObject.transform.localPosition.y,
0);
NPG01_ModulePoint controlPointScript = controlPointObject.AddComponent<NPG01_ModulePoint>();
controlPointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.BEZIERCONTROLE);
for (int i = 0; i < numberOfLinePoint; i++)
{
GameObject point = new GameObject();
point.name = "Curve Point " + i.ToString();
point.transform.parent = curveHolder.transform;
point.transform.position = Vector3.zero;
point.transform.localPosition = Vector3.zero;
point.transform.localRotation = Quaternion.Euler(Vector3.zero);
centerLinePoints.Add(point.transform);
NPG01_ModulePoint pointScript = point.AddComponent<NPG01_ModulePoint>();
pointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.BEZIERCURVES);
}
for (int i = 0; i < numberOfLinePoint; i++)
{
float t = (float)i / ((float)numberOfLinePoint - 1);
centerLinePoints[i].localPosition = QuadraticBezierCurves(startPoint.localPosition, endPoint.localPosition, controlPointObject.transform.localPosition, t);
GameObject rightPoint = new GameObject();
GameObject leftPoint = new GameObject();
rightPoint.transform.parent = curveHolder.transform;
rightPoint.name = "Right " + i;
rightPoint.transform.localPosition = centerLinePoints[i].localPosition + (Vector3.right * 3);
NPG01_ModulePoint rightPointScript = rightPoint.AddComponent<NPG01_ModulePoint>();
rightPointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.BEZIERCURVES);
leftPoint.transform.parent = curveHolder.transform;
leftPoint.name = "Left " + i;
leftPoint.transform.position = Vector3.zero;
leftPoint.transform.localPosition = Vector3.zero;
leftPoint.transform.localPosition = centerLinePoints[i].localPosition + (-Vector3.right * 3);
NPG01_ModulePoint leftPointScript = leftPoint.AddComponent<NPG01_ModulePoint>();
leftPointScript.updatePoint(NPG01_ModulePoint.POINTSTATE.BEZIERCURVES);
}
for (int i = 0; i < numberOfLinePoint; i++)
{
corridorAllPoint.Add(centerLinePoints[i]);
corridorAllPoint.Add(leftPoints[i]);
corridorAllPoint.Add(rightPoints[i]);
}
break;
case CORRIDORTYPE.CubicBezierCurve:
break;
}
}
private void MakeMeshData()
{
//mr = GetComponent<MeshRenderer>();
//mf = GetComponent<MeshFilter>();
GameObject meshHolder = new GameObject();
meshHolder.name = "Mesh Holder";
meshHolder.transform.parent = transform;
meshHolder.transform.localPosition = Vector3.zero;
meshHolder.transform.localRotation = Quaternion.Euler(Vector3.zero);
for (int i = 0; i < numberOfLinePoint - 1; i++)
{
GameObject leftFloorPanel = new GameObject();
GameObject rightFloorPanel = new GameObject();
leftFloorPanel.name = "Left Floor " + i.ToString();
rightFloorPanel.name = "Right Floor " + i.ToString();
leftFloorPanel.transform.parent = meshHolder.transform;
leftFloorPanel.transform.position = Vector3.zero;
leftFloorPanel.transform.localPosition = Vector3.zero;
leftFloorPanel.transform.localRotation = Quaternion.Euler(Vector3.zero);
Vector3 leftFloorPanelPostion = centerLinePoints[i].localPosition;
leftFloorPanelPostion.x = (leftPoints[i].localPosition.x + centerLinePoints[i].localPosition.x) / 2;
leftFloorPanelPostion.z = (centerLinePoints[i + 1].localPosition.z + centerLinePoints[i].localPosition.z) / 2;
leftFloorPanel.transform.localPosition = leftFloorPanelPostion;
rightFloorPanel.transform.parent = meshHolder.transform;
rightFloorPanel.transform.localPosition = Vector3.zero;
rightFloorPanel.transform.localRotation = Quaternion.Euler(Vector3.zero);
Vector3 rightFloorPanelPostion = centerLinePoints[i].localPosition;
rightFloorPanelPostion.x = (rightPoints[i].localPosition.x + centerLinePoints[i].localPosition.x) / 2;
rightFloorPanelPostion.z = (centerLinePoints[i + 1].localPosition.z + centerLinePoints[i].localPosition.z) / 2;
rightFloorPanel.transform.localPosition = rightFloorPanelPostion;
listOfCorridorFloor.Add(leftFloorPanel);
listOfCorridorFloor.Add(rightFloorPanel);
// Debug.Log(leftFloorPanel.transform.position + " " + leftFloorPanel.transform.localPosition);
}
corridorVertices = new Vector3[listOfCorridorFloor.Count * 3];
corridorTrangle = new int[listOfCorridorFloor.Count * 3];
//listOfCorridorFloor[0].transform.parent = null;
//centerLinePoints[0].transform.parent = null;
//leftPoints[0].transform.parent = null;
//leftPoints[1].transform.parent = null;
testVec[0] = centerLinePoints[0].transform.position;
testVec[1] = leftPoints[0].transform.position;
testVec[2] = leftPoints[1].transform.position;
//testVec[3] = leftPoints[1].transform.position;
//testVec[4] = centerLinePoints[1].position;
//testVec[5] = centerLinePoints[0].position;
textInt[0] = 0;
textInt[1] = 1;
textInt[2] = 2;
//textInt[3] = 3;
//textInt[4] = 4;
//textInt[5] = 5;
listOfCorridorFloor[0].AddComponent<MeshFilter>();
listOfCorridorFloor[0].AddComponent<MeshRenderer>();
}
private void Update()
{
Mesh testmesh = listOfCorridorFloor[0].GetComponent<MeshFilter>().mesh;
testVec[0] = centerLinePoints[0].transform.position;
testVec[1] = leftPoints[0].transform.position;
testVec[2] = leftPoints[1].transform.position;
textInt[0] = 0;
textInt[1] = 1;
textInt[2] = 2;
//Debug.Log("point local: " + centerLinePoints[0].transform.localPosition);
//Debug.Log("point global : " + centerLinePoints[0].transform.position);
//Debug.Log("vertice " + testVec[0]);
testmesh.Clear();
testmesh.vertices = testVec;
testmesh.triangles = textInt;
}
Vector3 LinearBezierCurves(Vector3 _pointStart, Vector3 _pointEnd, float t)
{// P0 P1
//P(t) = P0 + t * (P1 – P0) = (1 - t) P0 + t * P1 , 0 < t < 1
Vector3 p = Vector3.zero;
p = _pointStart + t * (_pointEnd - _pointStart);
return p;
}
Vector3 QuadraticBezierCurves(Vector3 _pointStart, Vector3 _pointEnd, Vector3 _pointControl, float t)
{ // P0 P1 P2
// P(t) = (1-t)^2 * P0 + 2(1-t) t * P2 + t^2 * P1 , 0 < t < 1
// uu u2 tt
Vector3 p = Vector3.zero;
float u = 1 - t;
float tt = t * t;
float uu = u * u;
float u2 = u * 2;
p = uu * _pointStart;
p += u2 * t * _pointControl;
p += tt * _pointEnd;
return p;
}
Vector3 CubicBezierCurve(Vector3 _pointStart, Vector3 _pointEnd, Vector3 _pointControlOne, Vector3 _pointControlTwo, float t)
{ // P0 P1 P2 P3
//P(t) = (1-t)^3 * P0 + 3 (1-t)^2 * t * P2 +3(1-t) * t^2 * P3 + t^3 * P1 , 0 < t< 1
// uuu uu u
Vector3 p = Vector3.zero;
float u = 1 - t;
float uu = u * u;
float uuu = u * u * u;
float tt = t * t;
float ttt = t * t * t;
return p;
}
private void MakeMeshData(Vector3 meshPointOne, Vector3 meshPointTwo, Vector3 meshPointThree, int index)
{
corridorVertices[index - 3] = meshPointOne;
corridorVertices[index - 2] = meshPointTwo;
corridorVertices[index - 1] = meshPointThree;
corridorTrangle[index - 3] = index - 3;
corridorTrangle[index - 2] = index - 2;
corridorTrangle[index - 1] = index - 1;
}
//private void CreateMesh()
//{
// roomMesh.Clear();
// roomMesh.vertices = roomVertices;
// roomMesh.triangles = roomTrangle;
//}
}
The mesh is always expressed in relation to its root transform. In order to use global positions use Transform.TransformPoint (or transform.InverseTransformPoint) to convert them to local space, i.e. all points must be expressed relative to the center of the gameobejct that will be displaying the mesh.

Specified cast is not valid

SqlDataReader reader;
string r="C:\\Users\\Shivam\\Documents\\";
if ((FileUpload1.PostedFile != null)&&(FileUpload1.PostedFile.ContentLength > 0))
{
r += System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
}
OleDbConnection oconn =
new OleDbConnection
(#"Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source="+r+";"
+ #"Extended Properties=""Excel 8.0;HDR=Yes;""");
oconn.Open();
conn.Open();
OleDbCommand dbcom = new OleDbCommand("SELECT * FROM [Sheet1$]", oconn);
OleDbDataReader dbreader = dbcom.ExecuteReader();
//dbread = dbreader;
int rni = dbreader.GetOrdinal ("RollNo");
int mki = dbreader.GetOrdinal ("marks");
int rowcount =0;
while(dbreader.Read())
{ rowcount++; }
//dbreader.Close();
//OleDbDataReader dbread = dbcom.ExecuteReader();
int[] rn = new int[rowcount];
int[] gr = new int[rowcount];
while (dbreader.Read())
{
int o = 0;
for(int i=0;i<rowcount;i++)
{
int q = (int)dbreader.GetValue(rni);
int p = (int)dbreader.GetValue(mki);
rn[i] = q;
gr[i] = p;
//roll[i] = valid(odr, 0);//Here we are calling the valid method
//marks[i] = valid(odr, 1);
//i++;
if (gr[i] >= 11)
{ o=i; }
}
if(o!=0)
{ break; }
TextBox4.Text += rn + "\t" + gr;
//Here using this method we are inserting the data into the database
x = TextBox2.Text.Substring(0, 1);
y = TextBox2.Text.Substring(1, 1);
//for (int s = 0; s < roll.Length; s++)
//{
//SqlDataAdapter sda = new SqlDataAdapter("select StudentID from Student where APID=" + int.Parse(y)+ "and Semester=" + int.Parse(z) + "and Roll_No=" + int.Parse(RollNo), conn);
//DataSet ds = new DataSet();
//sda.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
SqlCommand command = new SqlCommand();
command.Connection = conn;
//command.Connection.Open();
command.CommandType = CommandType.Text;
int c = rn.Length;
for (int n = 0; n<rn.Length; n++)
{
command.CommandText = "Select StudentID from Student where APID=" + int.Parse(x) + "and Semester=" + int.Parse(y) + "and Roll_No=" + rn[n];
}
reader = command.ExecuteReader();
while (reader.Read())
{
TextBox4.Text = reader.GetInt32(0).ToString();
a = (int)reader["StudentID"];
for (int v = 0; v < rn.Length; v++)
{
insertdataintosql(rn[v], gr[v]);
}
}
//}
}
conn.Close();
oconn.Close();
The problem here is that the statements in the while(dbreader.read()) are not executed, rather directly conn.Close() is performed. And if I take another datareader with same command after closing the previous datareader, the error "Specified cast not valid" is thrown at "int q = (int)dbreader.GetValue(rni);". Please help me out...thanks in advance
DataReaders are forward-only. You can't "reuse" it.
All you are doing is allocating an array based on the number of records. Either run a COUNT query first to get the number of records, or re-allocate your array on the fly in the one loop.
OleDbCommand dbcom = new OleDbCommand("SELECT COUNT(*) as RowCount FROM [Sheet1$]", oconn);
dbreader = dbcom.ExecuteReader();
dbreader.Read();
rowcount = dbreader.GetOrdinal("RowCount");
dbcom.Close();
dbcom = new OleDbCommand("SELECT * FROM [Sheet1$]", oconn);
dbreader = dbcom.ExecuteReader();
... carry on with your 2nd loop
There is more to say about releasing resources properly. Best practice is to add parameter CommandBehavior.CloseConnection to ExecuteReader, create your data reader inside a using() construct and issue cmd.Dispose() at the end to ensure SQL connection resources are released properly.
.. though actually since it's a local file you're using it may not matter so much but generally speaking you should do that. Otherwise its very easy to find that an orphaned DataReader has not released its connection.