|
Put the console.write and console.readkey INSIDE the for next loop. Move the } down 2 lines.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
i've tried that, it just writes the numbers straight after the entering, i want it to write all 5 AFTER i've entered the fifth number.
|
|
|
|
|
And that make no sense at all, you would have to put the numbers into a container (array/List<>) and then loop through the container and print them!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
problem in this code is that 'i' is only defined for 'for loop ' to make it run u have to define i before for loop
ex:
int i = new int();
for (i = 0; i < 5; i++)
{
i = int.Parse(Console.ReadLine());
}
Console.Write(i);
Console.ReadKey();
it will not give any error but don't give u the result u want it should be like this..
static void Main(string[] args)
{
int i = new int();
int []arry=new int[5];
for (i = 0; i < 5; i++)
{
arry[i] = int.Parse(Console.ReadLine());
}
for (i = 0; i < 5; i++)
{
Console.Write(arry[i]+"\n");
}
Console.ReadKey();
}
|
|
|
|
|
ok, I thought I could do it without the arrays, but thanks anyway
|
|
|
|
|
Just to add to what the others have said - you don't want to do it like that anyway!
for (int i = 0; i < 5; i++)
{
i = int.Parse(Console.ReadLine());
}
Since i is both the loop control variable and the place you store the value the user entered, if the user enters 4 or above as his first number, it will exit the loop immediately.
You need to separate these two functions into two variables, as well as using an array or List<int>
BTW: It is considered a bad idea to use "magic numbers" such as "5" in your code - when you move to an array, it is too easy to later change one and not the other, causing your application to fail, or crash:
int[] inp = new int[5];
for (int i = 0; i < 5; i++)
{
inp[i] = int.Parse(Console.ReadLine());
}
would be better as:
const int elements = 5;
int[] inp = new int[elements];
for (int i = 0; i < elements; i++)
{
inp[i] = int.Parse(Console.ReadLine());
} That way, you only have to change the number of elements in a single place, and it all works perfectly.
And as a final thing, if your user enters an alphabetic character instead of a digit, your program will crash. You should always check your user input - they do enter rubbish quite often!
[edit]Typos - OriginalGriff[/edit]
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
i wonder how this programs builds without any error?
there are few things you need to check here
1.the variable i is accessible only inside the loop and its local to that loop never accessible outside the loops.
2. the indexer variable i is modified against your input which may alter the loop execution
3. Console.Write is outside the loop so if variable i is acceible outside the loop it will print only the last entered value not all
try modifying your code.
define a new variable to store the read line
and move the Console.Write inside the loop and see the difference
Jibesh.V.P
India
|
|
|
|
|
I need some help to make a Order Form in C#. My development environment is:
Microsoft Visual Studio 2010 Ultimate Sql Server Express Edition 2005 Programming Language C# Sample Database = NorthWind (Tables=Orders and OrderDetails)
I've create a Form for order dataentry, which contain Textbox for OrderID, Combobox for Customer, DateTimePickers for OrderDate and ShippedDate and a DataGridView which contains Cokumns OrderID=ReadOnly, ProductID, UnitPrice & Quantity:
In the form load event I've the following code:
private void Inv2_Load(object sender, EventArgs e)
{
SetComb();
connectionString = ConfigurationManager.AppSettings["connectionString"];
sqlConnection = new SqlConnection(connectionString);
qryOrd = "Select OrderID, CustomerID, OrderDate, ShippedDate from Orders";
qryOrdDet = "Select OrderID, ProductID, UnitPrice, Quantity from OrderDetails";
sqlConnection.Open();
sqlDataMaster = new SqlDataAdapter(qryOrd, sqlConnection);
sqlDataDet = new SqlDataAdapter(qryOrdDet, sqlConnection);
command = new SqlCommand("INSERT INTO Orders ( CustomerID, OrderDate, ShippedDate) VALUES (@CustID, @OrdDt, @ShipDt) SELECT SCOPE_IDENTITY();");
command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15);
command.Parameters.Add("@CustID", SqlDbType.VarChar, 15);
command.Parameters["@CustID"].Value = cmbCust.SelectedText;
command.Parameters.Add("@OrdDt", SqlDbType.DateTime);
command.Parameters["@OrdDt"].Value = dtOrdDt.Text;
command.Parameters.Add("@ShipDt", SqlDbType.DateTime);
command.Parameters["@ShipDt"].Value =dtShipDt.Text;
sqlDataMaster.InsertCommand = command;
command = new SqlCommand("UPDATE Orders SET CustomerID = @CustID, OrderDate = @OrdDt, ShippedDate = @ShipDt WHERE OrderID = @OrdID");
command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15, "OrderID").Value = txtOrdID.Text;
command.Parameters.Add("@CustID", SqlDbType.VarChar, 15, "CustomerID").Value = cmbCust.Text;
command.Parameters.Add("@OrdDt", SqlDbType.DateTime).Value = dtOrdDt.Text;
command.Parameters.Add("@ShipDt", SqlDbType.DateTime).Value = dtShipDt.Text;
sqlDataMaster.UpdateCommand = command;
commandDet = new SqlCommand("INSERT INTO OrderDetails (ProductID, UnitPrice, Quantity) VALUES (@PrdID, @Up,@Qty)");
commandDet.Parameters.Add("@PrdId", SqlDbType.NVarChar, 5, "ProductID");
commandDet.Parameters.Add("@Up", SqlDbType.VarChar, 50, "UnitPrice");
commandDet.Parameters.Add("@Qty", SqlDbType.VarChar, 20, "Quantity");
sqlDataDet.InsertCommand = commandDet;
commandDet = new SqlCommand("UPDATE OrderDetails SET ProductID = @PrdID, UnitPrice = @Up, Quantity = @Qty WHERE OrderID = @OrdID");
commandDet.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15, "OrderID").Value = txtOrdID.Text; ;
commandDet.Parameters.Add("@PrdId", SqlDbType.NVarChar, 5, "ProductID");
commandDet.Parameters.Add("@Up", SqlDbType.VarChar, 50, "UnitPrice");
commandDet.Parameters.Add("@Qty", SqlDbType.VarChar, 20, "Quantity");
sqlDataDet.UpdateCommand = commandDet;
sqlComBldMaster = new SqlCommandBuilder(sqlDataMaster);
sqlComBldDet = new SqlCommandBuilder(sqlDataDet);
dt = new DataTable();
dtDet = new DataTable();
dt.Clear();
dtDet.Clear();
sqlDataMaster.FillSchema(dt, SchemaType.Source);
sqlDataDet.FillSchema(dtDet, SchemaType.Source);
dtDet.Columns["OrderID"].AutoIncrement = true;
dtDet.Columns["OrderID"].AutoIncrementSeed = -1;
dtDet.Columns["OrderID"].AutoIncrementStep = -1;
ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dtDet);
ds.EnforceConstraints = false;
DataRelation rel = new DataRelation("OrdersRel", ds.Tables["Orders"].Columns["OrderID"], ds.Tables["OrderDetails"].Columns["OrderID"]);
ds.Relations.Add(rel);
bs = new BindingSource();
bsDet = new BindingSource();
bs.DataSource = ds;
bs.DataMember = "Orders";
bsDet.DataSource = ds;
bsDet.DataMember = "OrderDetails";
dgInvDet.AutoGenerateColumns = false;
dgInvDet.Columns["ProductID"].DataPropertyName = "ProductID";
ProductID.DataSource = dm.GetData("Select * from Products order by ProductName");
ProductID.DisplayMember = "ProductName";
ProductID.ValueMember = "ProductID";
dgInvDet.Columns["UnitPrice"].DataPropertyName = "UnitPrice";
dgInvDet.Columns["Quantity"].DataPropertyName = "Quantity";
dgInvDet.DataSource = bsDet;
}
public void SetComb()
{
cmbCust.DataSource = dm.GetData("Select * from Customers order by CompanyName");
cmbCust.DisplayMember = "CompanyName";
cmbCust.ValueMember = "CustomerId";
cmbCust.Text = "";
}
"Dm.GetData" is the Data Access class method created for the purpose of just retrieving reocrds...
And in the Save button click event:
private void btnSave_Click(object sender, EventArgs e)
{
dt.EndInit();
rec = sqlDataMaster.Update(ds.Tables[0]);
rec += sqlDataDet.Update(ds.Tables[1]);
ds.AcceptChanges();
MessageBox.Show(rec + " record(s) applied...." );
ds.EnforceConstraints = true;
}
What I need is to save the Data to Sql Server in respective table (Orders and OrderDetails) which my code can't seems to do it and it shows error that Foreign Key cannot be null...Because OrderDetails table also needs OrderID which is Foreign Key, and I am unable to understand how can I get the OrderID, as it is Auto generated after data is inserted in Database.
Please help me on this problem to save the data in database with this foreign key issue...
Any help will be much appreciated.
Thanks
Ahmed
|
|
|
|
|
ahmed_one wrote: and I am unable to understand how can I get the OrderID, as it is Auto generated after data is inserted in Database.
You could select the last identity[^].
|
|
|
|
|
Thanks for reply,
My problem is OrderID is not generated because of error that child table is not saved..the whole save procedure is failed because of this and compiler shows the error.
|
|
|
|
|
ahmed_one wrote: My problem is OrderID is not generated because of error that child table is not saved..
Save the Order first, fetch the identity, save the child-records.
|
|
|
|
|
How will i continuous listen to COM for detecting modem.
if a modem is added or removed it should inform you that Modem installed or removed at COM45 etc.
I have tried to use a thread and check the com count but i am unable to know which com is modem.
If there is any specific class for this pls help me fr the code asap???
|
|
|
|
|
|
You can use a keyboard!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I'm not sure if I understand you: You want to iterate through all COM-Ports to detect, if a modem is connected?
You can open each COM-Port, send the command "AT" and wait if you get an "OK" as answer. Then you know, that there is a modem.
|
|
|
|
|
What is the most efficient way of returning or finding the largest int variable from an object. For example, object A has many int variables. I am just wandering is there a way to iterate through an object's variables without resorting to a long and drawn out if statement.
|
|
|
|
|
That depends on the object. Is it a collection of ints? Or will you have to use Reflection to find the members?
|
|
|
|
|
It's just a standard object with int properties; no collections. As a new programmer, not sure what reflection actually is.
|
|
|
|
|
Here[^] is some more information on reflection.
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
Wouldn't it have been better to hold the values in an array or collection instead of discrete variables??
|
|
|
|
|
Yeah, I just realized that after reading up on the whole reflection bit, lol. Thanks.
|
|
|
|
|
Hi,
I think n-1 times consideration is the best way in one loop.
Is there any other better idea?
modified 28-May-14 1:48am.
|
|
|
|
|
Hi,
I would like to serialize a class (Person),
That contains a field called "Spouse", of the same type(Person).
I'm having a bit of problem doing that because I don't know
when to stop writing.
I'm trying to do it manualy, not with XmlSerializer BinaryFormatter ect.
BTW
when I do try to serialize with the classes mentioned above, XmlSerializer - works fine only when I'm not serialize "Spouse" field.
with BinaryFormatter it works well.
public class Person
{
public int Age { get; set; }
public FullName Name { get; set; }
public Address Address { get; set; }
public Person Spouse { get; set; }
}
|
|
|
|
|
I am assuming that you have two Person objects and the Spouse field of each object points to the other one. You just need to keep a note when serializing to stop on the second object when you reach the Spouse field.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi Richard, and thanks for you quick answer.
what I'm trying to do is to save one Person object and because Spouse Field I need to save another Person object.
My serialize method should be generic, not specific to Persons objects, and therefore I don't know when to stop writing.
The method signature:
public static void Serialize<T>(T o, Stream stream)
a little brief of what i have done so far in this method:
by using reflection,whice means using:
Type.GetType(obj.Tostring());
GetProperties();
I'm scaning the props and each time a prop is not Primitive
type I'm doing the recursive call with this prop as a first parameter - it works well.
coming back to "stop writing issue" whice i dont know the generically solution for it.
Thanks.
|
|
|
|
|