|
See a recently posted article on how to handle this. Sorry no link, but it should be under recent still.
xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!
|
|
|
|
|
ok.. i have a function and it looks like this..
public static void test(ListBox lst,string tablename)
{
string myConn = "Server=(local);Integrated security=SSPI;database=halkdanis";
string str = "SELECT * FROM '"+tablename+"'";
SqlDataAdapter dataAdapter = new SqlDataAdapter(str,myConn);
DataSet dt = new DataSet();
DataAdapter.Fill(dt,tablename); // here it says error
DataTable dtb = dt.Tables[0];
foreach(DataRow dtr in dtb.Rows)
{
lst.Items.Add(dtr["username"]);
}
}
as u can see, i want to get data from mssql, and put the usernames in a listbox and i want to use this function several times depends on tablenames but it gives error at:
DataAdapter.Fill(dt,tablename) ;
i guess its coz of `"` char or dunno what is it..
any help would be great..
thx.
good coding..
god is a coder..
|
|
|
|
|
Don't enclose the table name in single quotes.
Better yet, use parameters:
string str = "Select * from @table";<br />
SqlDataAdapter dataAdapter = new SqlDataAdapter(str,myConn);<br />
dataAdapter.SelectCommand.Parameters.Add("@table", tablename);<br />
DataSet dt = new DataSet();<br />
dataAdapter.SelectCommand.Connection.Open();
DataAdapter.Fill(dt,tablename);
...<br />
Since you are just copying one colum value to a listbox, theis would be much more effieciently done with an SqlDataReader, and the select commnd should only ask for the one column:
string str = "Select [username] from @table";<br />
SqlConnection myConn + new SqlConnection("Server=(local);Integrated security=SSPI;database=halkdanis");<br />
SqlCommand cmd = new SqlCommand(str,myConn);<br />
cmd.Parameters.Add("@table",tablename);<br />
myConn.Open();<br />
SqlDataReader reader = cmd.ExecuteReader();<br />
try<br />
{<br />
while(reader.Read()) <br />
{<br />
lst.Add(reader.GetString(0));
}<br />
}<br />
finally<br />
{<br />
reader.Close();
myConn.Close();
}
This is much faster, and consumes much less working memory. It also transfers much less data 'over the wire' if the server is on a different box.
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
this is still not working...
string str = "Select [username] from @table";
SqlConnection myConn = new SqlConnection("Server=(local);Integrated security=SSPI;database=halkdanis");
SqlCommand cmd = new SqlCommand(str,myConn);
cmd.Parameters.Add("@table",tablename);
myConn.Open();
SqlDataReader reader = cmd.ExecuteReader(); // here it sais error
try
{
while(reader.Read())
{
lst.Items.Add(reader.GetString(0)); //since we only asked for one colume, just get string from col 0
}
}
finally
{
reader.Close();//close reader
myConn.Close();//close connection
}
and i also tried myStyle with parameters..
string str = "Select * from @table";
SqlDataAdapter dataAdapter = new SqlDataAdapter(str,myConn);
dataAdapter.SelectCommand.Parameters.Add("@table", tablename);
gives the same error
DataAdapter.Fill(dt,tablename); // error here..
|
|
|
|
|
What is the error? What are you passing in for tablename? Does the table exist?
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
-- modified at 22:45 Sunday 30th October, 2005
|
|
|
|
|
public static void test(ListBox lst, string tablename)
{
string str = "Select [username] from @table";
SqlConnection myConn = new SqlConnection("Server=(local);Integrated security=SSPI;database=halkdanis");
SqlCommand cmd = new SqlCommand(str,myConn);
cmd.Parameters.Add("@table",tablename);
myConn.Open();
SqlDataReader reader = cmd.ExecuteReader(); // error points here
try
{
while(reader.Read())
{
lst.Items.Add(reader.GetString(0)); //since we only asked for one colume, just get string from col 0
}
}
finally
{
reader.Close();//close reader
myConn.Close();//close connection
}
}
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error.
and i coulnt take take SqlDataReader reader = cmd.ExecuteReader(); in a try - catch block coz it gives `reader` couldnt find errors ..
my usage of the function is..
defaultCheck.test(empList,"employeeinfo"); // empList = listBox , "employeeinfo" = tablename
thx.
|
|
|
|
|
Are you sure your Sql Server is running? "system error' sounds rather bad, like something fundamental is wrong. Can you open the table in Enterprise Manager?
I don't understnd your comment about not being able to move the executereader() call into the try block --- are you saying that
public static void test(ListBox lst, string tablename)<br />
{<br />
string str = "Select [username] from @table";<br />
SqlConnection myConn = new SqlConnection("Server=(local);Integrated security=SSPI;database=halkdanis");<br />
SqlCommand cmd = new SqlCommand(str,myConn);<br />
cmd.Parameters.Add("@table",tablename);<br />
myConn.Open();<br />
<br />
try<br />
{<br />
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read()) <br />
{<br />
lst.Items.Add(reader.GetString(0));
}<br />
}<br />
catch(SqlException e)<br />
{<br />
System.Diagnostics.Trace.WriteLine(e.message);<br />
}<br />
finally<br />
{<br />
reader.Close();
myConn.Close();
}<br />
<br />
}
Doesn't throw an error?
Also, I'm more accustomed to seeing "Initial Catalog=halkdanis;" in an Sql Server connection string, rather than "database=" "database" might work, but then again it might not..
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
my mssql server and everything seems fine, this is just a function of the system, i can add, delete, update records on my program right now and there is no problem, what i wanted to do is, i'm using this code 5 times in the program but each time i have to specify the tablename , so i had to use 5 functions only the tablenames are different. and instead of doing like that i wanted to use only one function and take the tablename as a string so i could use it..
try
{
SqlDataReader reader = cmd.ExecuteReader(); // error points here
while(reader.Read())
...
when i do like this , it gives error like where the heck is the reader but
SqlDataReader reader = cmd.ExecuteReader(); // error points here
try
{
while(reader.Read())
...
no errors but when i wanted to use the function it gives sql error and points
SqlDataReader reader = cmd.ExecuteReader();
thanks for your help but i guess i have to use 5 function grrr
|
|
|
|
|
Try it more youre original way. Instaed of a parameter, build up the SQL string like this:
string str = "select [username] from [" + tablename + "]";
leave the rest of the code the same (just remove the add parameters part).
the square brackets around username are important, since "username" is a keyword. Your original version had single quotes around tablename, which was definitely a problem. The square brackets should work.
To fix the compile error with the reader inside the try block,
SqlDatareader reader = null;<br />
try<br />
{<br />
reader = cmd.ExeuteReader();<br />
...
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
-- modified at 23:22 Sunday 30th October, 2005
|
|
|
|
|
awesome !!! thank you so much.. it works !
|
|
|
|
|
I want to build a C# application with integrated acrobat reader. I will have a list of the left hand side which is a list of links of pdf files I have on my computer and when I click the required pdf file link on left, pdf file must display on the right hand side of the application inside a pdf reader. I am currently having 100 pdf files for my literature review ( I am a PHD student ) and it has reached a state where its unmanagable.
I am a pretty OKAy coder in C#. But I simply don't know how to go about this.
Any help will be highly aprreciated.
Regards,
Amit
|
|
|
|
|
there are alot of options available for you to read a pdf file. Infact Listing the PDF in your application is not a issue than reading a pdf file.
You can use Acrobat SDK & Documentation for this. Infact it is not OpenSource but i downloaded (for Free) this SDK once when i needed to include javaScripts in PDF Events.
Secondly Check ITextSharp (OpenSource), PDFToolKit (Price 299 $), or PDFLib (dont know price).
I recommend you to use Acrobat SDK or ITextSharp
R A M
|
|
|
|
|
Dear Ram,
Thank you very much for that. I think I will stick with the ItextSharp.
I can extend this to any application, like powerpoint, word and excel etc.??? I mean if my file is not pdf but also consists of .doc, .txt, or . ppt, .xls?
Regards,
Amit
|
|
|
|
|
yeh you can use this library for TEXT,HTML,PDF(CODECS ALSO INCLUDED LIKE WMF),RTF DOCUMENTS,XML,GZIP,TAR,ZIP.
I am not confirmed about PPT and DOC. but ITextSharp Includes the Bouncy Castle (open Source) Library which is related to Security you can create digital certificates and signatures using Abstract Syntex Notations like X 509 X9 more over you can use it for SMIME Implementation.
Regards
Rizwan Ahmed Malik
R A M
|
|
|
|
|
Hi all,
I want to expose some global strings/ints so I wondered should I be using public const int i = 5; or public static int i = 5; ? Thanks.
Regards,
gamehack
|
|
|
|
|
It depends. Will this data be used within a class instance ? If so, no need to make it static. Are static and const mutually exclusive ? I don't think so. I think it should be const no matter what.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thanks
|
|
|
|
|
Be aware that "const" is resolved like at compile time like a C/C++ define. This means that if you change the value, other assemblies will still have the original value. "const" is fabulous for internal data, but lousy otherwise.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
A constant can not be declared as static. All constants are static.
A constant is not a variable as such but rather a compiler directive. Unless the constant is an object (e.g. a string), it's never stored as data.
If you declare a constant:
const int foo = 42;
This line:
bar = foo;
will produce exactly the same code as:
bar = 42;
---
b { font-weight: normal; }
|
|
|
|
|
Hi there.
I am wondering how to draw on a form. I have one form (f1) that has a button. When that button is pressed a second form (f2) springs up. I want to be able to draw on that form (f2). How do I do that?
This is what I did:
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Black, 2);
Point p3 = new Point(40,125);
Point p4 = new Point(55, 125);
g.DrawLine(pen, p3, p4);
g.Dispose();
I don´t get anything from this so I was wondering if someone can please help me?
Thanks,
FJ
|
|
|
|
|
Always tdo the painting in the OnPaint method:
protected override void OnPaint(PaintEventArgs args) {
Graphics g = args.Graphics;
Pen pen = new Pen(Color.Black, 2);
Point p3 = new Point(40,125);
Point p4 = new Point(55, 125);
g.DrawLine(pen, p3, p4);
}
With this code the line will be drawn everytime the form is redrawn and thus make sure it won't go away when the form refreshes.
|
|
|
|
|
Thanks for the help.
Your version draws the line on the underlying form f1 instead of on f2 like I wanted. How do we fix that?
FJ
|
|
|
|
|
Place the code in form f2 instead of f1?
If the code needs to be in f1 then try binding the Paint event of f2 in f1.
|
|
|
|
|
Hi.
I have a stupid question about editing database tables. I have runtime created dataset dsStuff, and runtime created table "items" within it. "items" have primary key field - ID. I need to create modal dialog to edit row from "items" with given value of ID. And I can't figure out how to tell some textBox that I need to edit row with given ID.
Thanks in advance.
|
|
|
|
|
Pass the ID into the constructor for the dialog, assuming that all editing happens in the dialog.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|