|
If you are going to put the data into your business object (News ) then I do not suggest using a SqlDataAdapter to fill a DataTable /DataSet because all you are going is wasting time creating extra copies of the data that you will throw away. (The SqlDataAdapter uses a SqlDataReader internally to populate the DataTable /DataSet )
You could look at the SqlDataReader.HasRows[^] property.
At the top of your method replace
News objNews = new News(); with
News objNews = null; . Wait until you perform the ExecuteReader() then, inside your try block (replacing the while loop), do something like this:
if (objDr.HasRows)
{
objNews = new News();
objDr.Read()
objNews.NewsID = Convert.ToInt32(objDr["PKNewsID"]);
objNews.CategoryID = Globals.CheckDBIntForDBNulls(objDr["FKNewsCategoryID"]);
objNews.Title = objDr["NewsTitle"].ToString();
} I took out the while because it wasn't really doing anything useful. If you expect only one row to be returned then the above will be fine. If you expect more than one row returned and you are just looking for the results of the last row then your old while loop will work, but it suggests there is something wrong with the SQL that it is returning more than it should.
Now, if no rows are returned from SQL Server, a null is returned from the method.
Does this help?
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
|
|
|
|
|
Hi..
Im doing a user interface using C# web application program. When i add a new user to database, after adding it goes to the next page and i can choose to edit and confirm add user.. But my problem here is if i wanted to edit i created a edit button and goes to my previous page. How can i maintain the previos page particulars?? Coz i have tried clicking it and when it return to the previous page the particulars i fill in has lost.. Thankz.. =)
Smile alwyz =p
|
|
|
|
|
You can't maintain state between pages without using session variables, and you should avoid it. Instead, pass a parameter on the URL which your page then uses to restore it's state from the database.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
How collections (Queue/Stack/Hashtable...) store complex object?
Do they store object reference or object gets serialize?
Appreciate your help.
- ashish
|
|
|
|
|
I'm pretty sure they just store a reference to the object, I know that types such as int get boxed to be stored in a C# 1.1 container.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
It means if I dispose the object instance, the reference in Queue will return false values
Is there any way I can store the full object?
- ashish
|
|
|
|
|
AshishBasran wrote: It means if I dispose the object instance, the reference in Queue will return false values
Of course. No different to a vector in C++, if you store a pointer, don't delete it elsewhere.
AshishBasran wrote: Is there any way I can store the full object?
Don't dispose of stuff you want to keep. I don't see the problem here.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Depends on what you store in the collection. Value types (structs and other primitive data types except string) get boxed, which implicitly creates a copy of the original object. Reference types (classes) are stored by reference.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi, This is Deepak. Friends I need you help. I just want to open the View Source Code of a HTML File through C# Code. Please Help me ASAP.
Deepak
|
|
|
|
|
Open your HTML file as text should give you the source code.
Cheers!!!!
- ashish
|
|
|
|
|
Hi
I have an application that has many different forms. All these forms use a certain kind of a TreeView that has drag/drop rules, copy/paste rules and other functionalities. I'm trying to create one inherited TreeView control instead of doing this for every TreeView in my application. Where do I start? How do I go about accomplishing that?
Thank you for your help.
|
|
|
|
|
i think you can build a usercontrol, that ok.;)
|
|
|
|
|
hi, I have a question.
I want to draw a line on the panel but this line must be drawn above all the control in this panel.
my code doesn't work as my want.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Graphics g = e.Graphics;
g.DrawRectangle(new Pen(Color.Red,3),0,0,this.Width-10,this.Height-10);
//this rectange still draws under all control
}
thanks for help!
|
|
|
|
|
Any control on your panel will draw above the panel. If this were not so, then you wouldn't be able to see your controls at all.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
so like you say, how can i put a frame(drawing image) may be this image on another or same panel which is displaying images that are capturing by webcam.
sorry about my bad English!
-- modified at 0:16 Wednesday 26th October, 2005
|
|
|
|
|
Are you doing this in the FORM's on paint, or a control's (what is this)? the co-ordinates you specify should draw the rectangle positionned at the top left of this and 10 pix smaller than the control's size - i.i the top left corner of the rectangle will be at the same point as the top left corner of the control's client area, with it's bottom right corner 10 pix in from this's botoom right corner...
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 panel still has alot controls and i want this rectangle draw above these controls,and i have just done it buy set the BackColor=Color.Transparency;
but while my panel display images capture by webcam, this doesn't qork anymore?
Sorry about my bad English!
thanks for your help!
|
|
|
|
|
Hi there,
i try this in my code but some problem in the updated stmt can u help me shoot wht the problem is the compiler thro me error: The name Var1 does not exist in the class or namespace of the project....
here is the codesnippet:
string strsql;
DataTable dt;
SqlDataAdapter da;
SqlConnection objConn = new SqlConnection("data source=yusuff;user id=sa;password=sa;initial catalog=Heritage");
dt = new DataTable();
strsql = "select * from Articles_production";
da = new SqlDataAdapter(strsql,objConn);
da.Fill(dt);
if(dt.Rows.Count > 0)
{
//foreach (DataRow dr in dt.Rows) //ervy loop
for (int i=0;i
|
|
|
|
|
If your code looks the way that it is posted. the for loop is incomplete.
|
|
|
|
|
no i posted my codesnippet as
For (int i=0;i
|
|
|
|
|
ur text box is truncate the code...pls check the code project page
regards
ybasha
|
|
|
|
|
ybasha wrote: 'where identifier='" + Id +"'";
Unless your unique ID is a string, you need to remove the quotes.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Since you didn't cut an paste the real code (as evidinced by the incomplete for statement) it is possible that the error message is correct: Var1 doesn't exist in the posted code. its var1... C# is after all case sensitive...
This would be better done as a parameterized query anyway. Parameters have fewer issues with text characters (don't mind unescaped single quotes for instance) and so are less likely to give strange errors than dynamically built SQL.
I notice that two or more of your replacements are replaceing unicode characters with double quotes, which likely totally corrupt the concatenated query sting (causing it to appear to terminate early). Using parameters would avoid that issue as well.
Christian's observation is also likely a problem, and is another that would be avoided by using parameters.
Building up SQL statements by concatenating stuff like this is bad practice for a number of reasons (increased garbage generation from all the string reallocations, vulnerability to SQL injection attack, etc).
[edit] On top of all that, all the string variables (var1, var2, etc) are declared inside the scope of the for loop, and so do not exist outside of it (where your sql sting is declared and concatenated)...move the declarations to before the for loop, and this might have a chance [edit]
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 0:00 Wednesday 26th October, 2005
|
|
|
|
|
It looks like Var1 is outside the scope of the loop.
|
|
|
|
|
did you spell the variables correctly (i.e Var1 != var1)?
|
|
|
|