|
Hi All, I am trying to replace the text of various radiobuttons with data stored in a database. Since this is not a Windows Form application, I had to use the using keyword to create a datatable and because of that the datatable is not visible outside of the using code block. Please take a look at my code below.
string Con = ConfigurationManager.ConnectionStrings["myCon"].ConnectionString;
using (OleDbConnection OleCon = new OleDbConnection())
{
OleCon.ConnectionString = Con;
OleCon.Open();
using (OleDbDataAdapter a = new OleDbDataAdapter("SELECT * FROM CustomerTable", OleCon))
{
DataTable myDataTable = new DataTable();
a.Fill(myDataTable);
}
}
int i =1;
while(i<myDataTable.Count)
{
i++;
}
I get the error message saying that myDataTable does not exist in the current context. How do I make it visible to code outside of the using block? Thanks in advance for your reply.
|
|
|
|
|
Declare it before the using statement.
|
|
|
|
|
You decalre it outside of the using block.
ASPnoob wrote: Since this is not a Windows Form application, I had to use the using keyword to
create a datatable
Uhhh, whether or not this is a Windows Forms app has nothing to do with using a using block. It's a stucture that can be used in any type of application and if you can use it or not depends on the object that your specifying in the using statement. That object must implement the IDisposable interface.
|
|
|
|
|
Hi All,
I am trying to add an element from List1 to List2 if it is not already in List2. The following is what I have and I'm getting the error message List1 is a variable but is used like a method.
List<RadioButton> List2 = new List<RadioButton>();
List<RadioButton> List1 = new List<RadioButton>();
List1.Add(0,null);
List1.Add(1,RadioButton1);
List1.Add(2,RadioButton2);
List1.Add(3,RadioButton3);
List1.Add(4,RadioButton4);
int listIndex = RandNum.Next(1, 4);
int i=1;
while(i<List1.Count)
{
if (!List2.Contains(List1(listIndex )))
{
List2.Add(List1(listIndex ));
i++;
}
}
Please explain what I am doing wrong. Thanks in advance.
modified 20-Oct-12 8:59am.
|
|
|
|
|
Your calls to the Add() method are trying to pass two parameters but Add() only takes one, a reference to a radio button. Also, in the lines at the end starting with
if (!List2.Contains(List1(listIndex )))
your syntax is incorrect for an indexing operation, it needs square brackets not parentheses, thus:
if (!List2.Contains(List1[listIndex]))
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi, thanks for replying. I guess the Insert method is what I need if I wanted to add value to a list at a specific index.
|
|
|
|
|
Sounds about right.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I have navigate a website that have a script to print the page on login. when i close the page via code before print dialog shown , the webbrowser shows the print dialog even the page is already closed.
the script use : Document.print()
how to kill thread comes from the page on closing.
i need to close the page and close the print dialog.
please help
|
|
|
|
|
You can't. Once it's presented to the user there is no way for your code to close the print dialog.
|
|
|
|
|
I make the windows form on which on a single button click performs like this
private void button1_Click(object sender, EventArgs e)
{
Excel.Application exapp=null;
Excel.Workbook exbook=null;
Excel.Worksheet exsheet = null;
object misvalue = System.Reflection.Missing.Value;
exapp = new Excel.Application();
exapp.SheetsInNewWorkbook = 1;
exbook = exapp.Workbooks.Add(misvalue);
exsheet = (Excel.Worksheet)exbook.Worksheets.get_Item(1);
exsheet.Cells[1, 1] = "First Name";
exsheet.Cells[1, 2] = "Last Name";
exsheet.Cells[1, 3] = "Address1";
exsheet.Cells[1, 4] = "Address2";
exsheet.Cells[1, 5] = "City";
exsheet.Cells[1, 6] = "PinCode";
exsheet.Cells[1, 7] = "Phone Number";
exsheet.Cells[2, 1] = textBox1.Text.ToString();
exsheet.Cells[2, 2] = textBox2.Text.ToString();
exsheet.Cells[2, 3] = textBox3.Text.ToString();
exsheet.Cells[2, 4] = textBox4.Text.ToString();
exsheet.Cells[2, 5] = textBox5.Text.ToString();
exsheet.Cells[2, 6] = textBox6.Text.ToString();
exsheet.Cells[2, 7] = textBox7.Text.ToString();
exbook.SaveAs("rep.xls", Excel.XlFileFormat.xlWorkbookNormal, misvalue, misvalue, misvalue, misvalue, Excel.XlSaveAsAccessMode.xlExclusive, misvalue, misvalue, misvalue, misvalue, misvalue);
exapp.Quit();
releaseObject(exapp);
releaseObject(exbook);
releaseObject(exsheet);
}
but this thing imports data from windows in only one sheet
but i want to make a new sheet one each button click in excel
can anybody help me in it??????
Thanks in advance
|
|
|
|
|
I guess you need to create your workbook outside of this method. Then, when this method gets called it should add a new sheet to the workbook, fill that sheet with the data and save the workbook. You can then close the workbook at the end of the program.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
Excel.Application exapp=null;
Excel.Workbook exbook=null;
Excel.Worksheet exsheet = null;
object misvalue =System.Reflection.Missing.Value;
string fname = Application.StartupPath + "rep.xls";
exapp = new Excel.Application();
if (!File.Exists(fileToOpen))
{
exbook = (Excel._Workbook)(exapp.Workbooks.Open
(fileToOpen,0, true, 5, _value,_value, false,Excel.XlPlatform.xlWindows, _value,false, true,_value, _value,_value, _value));
}
else
{
exapp.SheetsInNewWorkbook = 1;
}
exbook = exapp.Workbooks.Add(misvalue);
exsheet = (Excel.Worksheet)
exbook.Worksheets.get_Item(exbook.Sheets.Count);
exsheet.Cells[1, 1] = "First Name";
exsheet.Cells[1, 2] = "Last Name";
exsheet.Cells[1, 3] = "Address1";
exsheet.Cells[1, 4] = "Address2";
exsheet.Cells[1, 5] = "City";
exsheet.Cells[1, 6] = "PinCode";
exsheet.Cells[1, 7] = "Phone Number";
exsheet.Cells[2, 1] = textBox1.Text.ToString();
exsheet.Cells[2, 2] = textBox2.Text.ToString();
exsheet.Cells[2, 3] = textBox3.Text.ToString();
exsheet.Cells[2, 4] = textBox4.Text.ToString();
exsheet.Cells[2, 5] = textBox5.Text.ToString();
exsheet.Cells[2, 6] = textBox6.Text.ToString();
exsheet.Cells[2, 7] = textBox7.Text.ToString();
if (!File.Exists(fileToOpen))
{
exbook.SaveAs(fileToOpen,Excel.XlFileFormat.xlWorkbookNormal, misvalue, misvalue, misvalue, misvalue, Excel.XlSaveAsAccessMode.xlExclusive, misvalue, misvalue, misvalue, misvalue, misvalue);
}
else
{
exbook.Save();
}
exapp.Quit();
releaseObject(exapp);
releaseObject(exbook);
releaseObject(exsheet);
}
|
|
|
|
|
thnks zaher but it doesnt work.....
|
|
|
|
|
Hi All,
I am access my computer window. when i click on my computer icon than open my computer window but when again click on my computer icon than same window open .My actual problem is , i want to not open same my computer window click on My computer Icon. same window means - first open My computer window content is C: , D: , E: Drive and again open second window content is C: , D: , E: Drive (both my computer window views are same) . I Want to do not open same view of My computer window.
Thanks
Aniruddha
|
|
|
|
|
1) What does this have to do with C#?
2) What do you expect to happen? If you think of the "My Computer" icon as a "door" into the "My Computer" "room", then why would you expect that the first time you open the door, it is "My Bedroom" and the second time it is "My Kitchen"?
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Then don't click on it a second time. Why do you believe something different should happen?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
This has absolutely nothing to do with C# or programming in general.
But, what you describe is expected behavior and you can't modify it.
|
|
|
|
|
In a C# 2010 console application it work works fine until it gets to a certain spot. Basically this console application calls another console application.
Here is what is happening:
1. When I run the console application, it runs ok until get reeaches a certain point. At that point is it trying to run the second console application from an directory path I had a week ago. Then I get error messages from the second console application.
2. Also it looks like I am the only person who can run this application. The application starts and never continues for other people.
I am thinking my problem must be in some file in the C# 2010 solution file.
Thus can you tell me what I can do to resolve my problem?
|
|
|
|
|
rachel_m wrote: Thus can you tell me what I can do to resolve my problem? Not without a lot more information. Remember, we cannot see what happens when you run your program, and we have no idea what it is trying to do. At the very least you should show the error message that you receive.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
The error messages says it can not find the specified directory location. However the name of the seond progam that is called is from an old location where the second program used to be at. However that is not the location where I am speficying that the second program should be executed from.
I have no idea how the first program calls the second wrong with the wrong directory path. Do I need to keep the path of the second console application static?
|
|
|
|
|
Rather than paraphrasing what you think is happening, why not show us the exact code that calls the second program, and the exact error message that gets produced. Without specific details of what is going on it is impossible for us to do any more than make guesses.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
There can be any one of a million things wrong. We don't have enough information to tell you anything useful at all.
|
|
|
|
|
The error messages says it can not find the specified directory location. However the name of the second progam that is called is from an old location where the second program used to be at. However that is not the location where I am speficying that the second program should be executed from.
I have no idea how the first program calls the second wrong with the wrong directory path. Do I need to keep the path of the second console application static?
|
|
|
|
|
There still isn't enough information.
From the error message, the path that you supplied to Process to start doesn't exist. Whatever you think it tried to launch is not what's important here. It's what the code thought when it tried to launch this second process.
You have to step through the code in the debugger or instrument the code to log what it's doing and what key variable values are to a file.
|
|
|
|
|
Are you and this person working on the same code? Link
|
|
|
|