|
hello guys... I have this DB in which I insert, update and delete data successfully. But problem comes when I try to add my own columns in Datagridview (Columns: Collection) and use the same code to get the data in it. But this does not show data in the columns already there and adds the new columns instead. Here is the code that I am using.
SqlConnection connection = new SqlConnection("Data Source=myServer; Initial Catalog=myDB; Integrated Security=True");
SqlDataAdapter adapter = new SqlDataAdapter("my SP name", connection);
DataTable dt = new DataTable();
try
{
connection.Open();
adapter.Fill(dt);
dg1.DataSource = dt;
}
catch (Exception ex)
{ connection.close(); MessageBox.Show(ex.Message);}
finally
{ connection.Close(); }
What can I do to add my own columns, but get the same data there in my own columns. thnx
|
|
|
|
|
You'll need to have everything you want in DataTable.
|
|
|
|
|
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.Int32");
myDataColumn.ColumnName = "id";
myDataColumn.ReadOnly = true;
myDataColumn.Unique = true;
myDataTable.Columns.Add(myDataColumn);
Source[^]
|
|
|
|
|
Thank you so much for your help. Now it is giving me new errors for something else and I dont understand. Does anyone know?
public static void Main()
{
stringinput,firstname,lastname;
double idium;
int password, choice;
string name;
Console.WriteLine("Enter your firstname:");
Console.WriteLine("Enter your ID Number:");
input = Console.ReadLine();
FirstName = Convert.ToString(input);
Console.WriteLine("Enter your lastname:");
input = Console.ReadLine();
LastName = Convert.ToString(input);
}
public static void checkout()
{
Console.Write("You are ready to check out.");
double[] payRate;
payRate = new double[4];
payRate[0] = 3.00;
payRate[1] = 4.15;
payRate[2] = 6.42;
payRate[3] = 7.95;
for(int x = 0; x < 4; ++x)
Console.WriteLine("Pay rate {0} is {1}", x, payRate[x].ToString("C"));
}
}
|
|
|
|
|
I will agree with Pete in your last posting - "This is fairly basic stuff". Perhaps you could look into a few online tutorials? That would probably help you to learn this better than asking several questions here.
Rather than trying to make a given block of code work, I would advise to learn the fundamentals instead...
|
|
|
|
|
Apart from the errors (tip: C# is case-sensitive), what's up with this?
Brian Reiber wrote: input = Console.ReadLine();
LastName = Convert.ToString(input);
It's already string, why convert a string to a string?
ILSpy confirms that the overload of Convert.ToString that takes a string argument just returns that string:
public static string ToString(string value)
{
return value;
}
|
|
|
|
|
Something else that you have failed to do is to show the actual error message that you receive. Don't expect others to be able to see what's happening on your PC when you try compiling this.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
What errors are you getting?
Brian Reiber wrote: stringinput
I suspect you mean string input
Brian Reiber wrote: input = Console.ReadLine();
FirstName = Convert.ToString(input);
Why not simply FirstName = Console.ReadLine(); ?
Brian Reiber wrote: Convert
I recommend never using Convert other than Convert.ChangeType
Brian Reiber wrote: {1} ... , payRate[x].ToString("C")
Try using {1:C} instead of the ToString.
|
|
|
|
|
Brian:
I am going to echo the recommendations of another post. As a new (and I really cannot refer to myself as a developer, yet) I researched online tutorials and books that would provide a foundation for C#, and VS 2010.
The following books have proven to be valuable resources:
1. Beginning C# Object Oriented Programming by Dan Clark (READ Chapter 5)
2. Visual C# 2010 Recipes by Jones and Freeman
3. HeadFirst C# by Stellman and Greene
4. Programming Language Pragmatics by Scott (buy it and put in on the bookshelf for a goal in understanding). It's beyond new developers, but it's a good feeling to go to the book and "get it" after study.
5. Herding Cats - A Primer for Programmers Who Lead Programmers by Rainwater (this book helps me understand those in the world of programming, as I come from the academic "world").
6. Online tutorials available from Lynda.com
My mentor (who has been programming since God was a child) says "Work on your errors for 2 hours, THEN ask for help".
My greatest hurdle has been in understanding the vocabulary that is used, and I finally had to stop obsessing... It is what it is.
Oh...Petzold's free online book (263 pages)was recommmended to me(Richard?), and is proving to be interesting, but I don't do well with books I cannot markup with pencil notations.
Two other books have been recommended, but they are on my Amazon wish list. Code Complete, and New Programmer's Survival Manual.
Looking at your code (I c/p'd into VS, and messed around with it), I was able to reduce the errors to 3, but then was stumped by my own ignorance...and I am not cheeky enough to make suggestions for changes. So your code helped me learn, so thank you.
Best of luck.
|
|
|
|
|
Deborah Palmer McCain wrote: I don't do well with books I cannot markup with pencil notations.
You can always print all or part of a PDF.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Yes, I read through part the book, and will be printing it so I can mark it. Truly a valuable resource, thank you again.
Deborah
|
|
|
|
|
Please give your questions a meaningful subject. Every question posted in this forum is from someone looking for 'C# Help'.
If you are getting error messages, first of all examine them and they will normally give you enough information to fix them! I recommend turning on line numbers in Visual Studio if you haven't already: Tools|Option|Editor|C# and check the Line numbers box.
Secondly, use a search engine of your choice and search for the error message (excluding any parts specific to your project).
Thirdly, if you still need help, post the error message here and indicate on which line of your code posted it actually fails on.
These will save people who wish to help you a lot of time and will make it far more probable that you will get a helpful answer.
|
|
|
|
|
|
|
OK, you've had a bit of time to fix your errors and enough hints...
Here is working code with
a) reduntant items removed
b) code styling and variable/method names changed where appropriate
c) other suggestions applied
d) the double array moved outside of the method - unless it's going to be different every time (as it's hardcoded I assume not) then it makes no sense to keep it there
Study it and the differences and see if you can learn from it:
private static readonly double[] PayRate =
new double[4] {
3.00,
4.15,
6.42,
7.95 };
static void Main()
{
Console.WriteLine("Enter your forename:");
string forename = Console.ReadLine();
Console.WriteLine("Enter your surname:");
string surname = Console.ReadLine();
Checkout();
Console.ReadKey();
}
private static void Checkout()
{
Console.WriteLine("You are ready to check out.");
for (int i = 0; i < PayRate.Length; i++)
Console.WriteLine("Pay rate {0} is {1:C}", i, PayRate[i]);
}
|
|
|
|
|
I am working on this particular part of code in c# and I keep receiving error messages and I dont know what I am doing wrong. Can anyone help me out?
public static void personalDetails()
{
string [,] books =
{"The Time Machine, Science Fiction, By H.G.Wells",3};
{"Ender's Game, Science Fiction, By Orson Scott Card",1};
{"Brave New World, Science Fiction, By Aldous Huxley",3};
{"Frankenstein, Horror, By Stephen King",1};
{"Misery, Horror, By Stephen King",2};
{"The Dark Half, Horror, By Stephen King",2};
{"Firestarter, Horror, By Stephen King",3};
{"Torment, Mystery, By Lauren Kate",3};
{"Before I fall, Mystery, By Lauren Oliver",3};
{"Entwined, Mystery, By Heather Dixon",2};
{"The Thirteenth Chime, Mystery, By Heather Dixon",1};
{"My Sister's Keeper, Mystery, By Jodi Picoult",3};
{"Harry Potter and the Deathly Hallows(Harry Potter,#7), Mystery,By J.K.Rowling",2};
{"The Da Vinci Code, Mystery, By Dan Brown",2};
int book
const MAX_BOOK = 5;
if(book >=5)
Console.WriteLine("You are at your limit!")
}
|
|
|
|
|
There are several problems here:
const int MAX_BOOK = 5;
public static void personalDetails()
{
string[,] books = {
{ "The Time Machine, Science Fiction, By H.G.Wells", "3" },
{ "Ender's Game, Science Fiction, By Orson Scott Card", "1" } };
int book = 0;
if (book >= MAX_BOOK)
Console.WriteLine("You are at your limit!");
}
|
|
|
|
|
Where to start? Well, you haven't initialised your array, so the first line needs to become:
string [,] books = { Next, you are adding in an integer into each line but it's expecting 2 strings so you should make it:
{"The Time Machine, Science Fiction, By H.G.Wells","3"}, As you are using an array initializer here, you need to end it with another } because you are adding in multiple items. I've already taken care of the first one for you at the top, so you'd end your initialisation with:
{"The Da Vinci Code, Mystery, By Dan Brown","2"}}; Note that the lists in the array must be separated by commas and not semi-colons.
int book needs to be terminated with a semi-colon as in:
int book; Beyond that, you declare a constant but don't tell it what type the constant is, so try
const int MAX_BOOK = 5; You haven't terminated your Console.WriteLine with a semi-colon. Make this:
Console.WriteLine("You are at your limit!"); Now, some general comments. First of all, you declare book but you don't set it to a value so it will never trigger the WriteLine condition because an int defaults to 0. I assume you mean to assign it to be the length of the array, so you actually need to use that value in there (hint - you need to use books.Length).
Secondly, you have declared a constant that you don't use - this is not a great idea; use the constant in place of the 5 in the if test.
Honestly, this is fairly basic stuff. You have to learn to read the compiler output and figure this stuff out. I saw the problems in your code just by looking at it - I didn't have the advantage of the compiler errors here; you do, so learn to use them. If you're using Visual Studio, the errors will be underlined and they will tell you exactly what's wrong.
|
|
|
|
|
Pete O'Hanlon wrote: you declare book but you don't set it to a value so it will never trigger the WriteLine condition because an int defaults to 0.
Worse actually, it will fail to compile. It doesn't satisfy the definite assignment rules (it doesn't even try).
|
|
|
|
|
I'm not sure if there is a better forum for this one - but I will put it here because the plug-in is written in C#...
I have an Outlook 2010 plug-in that I created, with a deployment solution. After installing it via the MSI file, there is a configuration file that must be manually copied to its installation folder,
C:\Program Files\<Company Name>\<Product Name>
The odd thing is, one of my colleagues reported that his file "vanished" from the folder. Today, the same thing happened on my machine. Both are running Windows 7 (one machine is 32-bit, the other 64). I checked my Recycle Bin, and it was not there either.
Does Windows 7 have some protective feature that removes files from protected directories automatically? I do know that Administrator rights are required to copy to that location.
|
|
|
|
|
<System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.WaitHandle.WaitOneNative(SafeWaitHandle waitHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext) at System.Threading.WaitHandle.WaitOne(Int64 timeout, Boolean exitContext) at System.Threading.WaitHandle.WaitOne(TimeSpan timeout, Boolean exitContext) at System.Threading.WaitHandle.WaitOne(TimeSpan timeout) >
Exception like this.
The web application, here on clicks, singleton object creates separate thread then that thread read a data read from xml file. using XMLDocument.
then after read that file data updated to database, then waiting for another request for that, with in the thread ManuelResetEvent wait is performed, if comes next request from while onlick button its Ret and release the thread block and doing that same process.
as per requirement,In between if needs, can change xml value dynamically, but when i edit that xml and saving, the thread was aborting automatically then entire application crashed.
code is
private void XMlreaderProcess()
{
long delayTimeSpanTicks = 0;
try
{
while (Active)
{
try
{
triggerCtrl.Reset();
if (delayTimeSpanTicks == 0)
{
triggerCtrl.WaitOne();
}
else
{
triggerCtrl.WaitOne(new TimeSpan(delayTimeSpanTicks));
}
StartProcess();
}
catch(Exception ex)
{
delayTimeSpanTicks = 0;
}
finally
{
}
}
}
catch(Exception ex)
{
}
finally
{
Active= false;
}
|
|
|
|
|
Not wishing to touch your design (which sounds pretty dodgy), just explain this exception: A ThreadAbortException is raised within a thread when Thread.Abort is called on it. Either your code, or some framework code, must be doing that. My guess would be that the web framework kills any threads that were started as the result of a request once the request is finished, but it is just a guess – hopefully someone can post who actually knows the answer.
Your posted code is swallowing exceptions – and it also appears to be using exceptions as part of normal code flow control, which is probably worse.
|
|
|
|
|
Thank for reply.
This is not based on web request process. This is schedule based background work. the user can update the time of the scheduling from web page.
on the application host the thread initiate the process.At beginning the thread started and waiting for that particular schedule time( which is getting from database)And that thread is waiting until that time using manualResetevent ( block the thread process using waitOne(milSec).
If comes at the particular times, the process is read the xml file and that data would be updated to database. for that xml reading
XmlDocument configDoc = new XmlDocument();
XmlNode parentNode = null;
configDoc.Load(tConfigfilePath);
if (null != configDoc)
{
XmlNode Id = configDoc.SelectSingleNode("/CCC/AAA");
Id.SetValue("ZZZ", detectOid.InnerText);
}
Then that value inserted to database. Again that waiting for next day time using below code. In between time if any changed happened on the xml file, suddenly the catch ( with in a while loop) is catch the exception.
private void XMlreaderProcess()
{
long delayTimeSpanTicks = 0;
try
{
while (Active)
{
try
{
triggerCtrl.Reset();
if (delayTimeSpanTicks == 0)
{
triggerCtrl.WaitOne();
}
else
{
triggerCtrl.WaitOne(new TimeSpan(delayTimeSpanTicks));
}
StartProcess();
}
catch(Exception ex)
{
delayTimeSpanTicks = 0;
}
finally
{
}
}
}
catch(Exception ex)
{
}
finally
{
Active= false;
}
|
|
|
|
|
Hi,
How to create a license file or digital signature for a setup file
on c#.
|
|
|
|
|
Hi All
I know this may not be the right place for this but can't see an appropriate place and hope someone is familiar with the issue i am having.
I have a c# app which i am deploying using studio 2008 deployment project.
Thing is i will be installing a 'loose' file and I only want to install it if the file does not exists on the target location or is newer than the one that does already exist.
A simple task i thought!! but i can't find any worthwhile help on this anywhere (probably googling wrongly).
Be very grateful if anyone can help me with this.
Thanks in advance
|
|
|
|
|