|
I have a command button that I basically want it to do one thing the first time, and something else on subsequent clicks.
I have a datagrid and the user clicks a button to add a line. The first time this happens, I need to go thru the BindData method to get everything initialized.
For subsequent clicks, I just want it to add the lines. I don't want to have to go through a postback, because I either end up with the first added line reset or I get the object reference not set to an instance error.
private void Button2_Click(object sender, System.EventArgs e)<br />
{ <br />
<br />
DataGridItemCollection items = dgParts.Items;<br />
int rows = items.Count;<br />
if (rows == 0)<br />
{<br />
BindData();<br />
DataTable dt = ds.Tables["Rest"];<br />
DataRow row;<br />
row = dt.NewRow(); <br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();<br />
}<br />
<br />
else<br />
{<br />
DataRow row;<br />
row = dt.NewRow(); <br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
}
When I click the button the second time my row count does say it has one row and tries to run through the else statement, but I get the null error thing.
I've been stuck on this for several days so if someone has any idea of a solution you will be my hero!
|
|
|
|
|
That is because you haven't assigned any value to the dt variable.
---
b { font-weight: normal; }
|
|
|
|
|
I have a new goofy error. When I hit button 2 (the if/else button I'm referring to) the code does not hit it. When I change the code back to what I had (which was not an if/else) it hits the code
Old code:
private void Button2_Click(object sender, System.EventArgs e)<br />
{ <br />
BindData();<br />
DataTable dt = ds.Tables["Rest"];<br />
DataRow row;<br />
row = dt.NewRow(); <br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();
New code that it doesn't call:
DataGridItemCollection items = dgParts.Items;<br />
<br />
int rows = items.Count;<br />
if (rows == 0)<br />
{<br />
BindData();<br />
DataTable dt = ds.Tables["Rest"];<br />
DataRow row;<br />
row = dt.NewRow(); <br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();<br />
}<br />
<br />
else<br />
{<br />
DataTable dt = ds.Tables["Rest"];<br />
ds = (DataSet) Session["ds"];<br />
DataRow row;<br />
row = dt.NewRow(); <br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();<br />
}
Previously I got the program to at least call the if/else when I was messing around with the code.
I do not have BindData in my Page_Load function because I want to call it when I actually need it. Does this have something to do with it?
|
|
|
|
|
Is it just the else block that isn't getting hit? Would it hurt to put the BindData call inside the else block?
|
|
|
|
|
The entire button2_click is not getting called. I went line by line in the debugger. In the one I wrote out, it goes through as expected. But the one with the if/else, it doesn't even look at it. Okay, must have had something commented because now it at least runs through the if/else. Still getting the null error.
This is what I now have:
private void Button2_Click(object sender, System.EventArgs e)<br />
{ <br />
<br />
DataGridItemCollection items = dgParts.Items;<br />
<br />
int rows = items.Count;<br />
if (rows == 0)<br />
{<br />
BindData();<br />
DataTable dt = ds.Tables["Rest"];<br />
DataRow row;<br />
row = dt.NewRow(); <br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();<br />
}<br />
<br />
else<br />
{<br />
DataTable dt = ds.Tables["Rest"]; ds = (DataSet) Session["ds"];<br />
DataRow row;<br />
row = dt.NewRow(); <br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();<br />
}
It bombs out at the line bolded.
-- modified at 14:00 Thursday 6th July, 2006
|
|
|
|
|
leckey wrote: must have had something commented
Oops, that happens
|
|
|
|
|
leckey wrote: Still getting the null error.
Is it because of ds? I do not see it being declared or initialized anywheres. You may want to hand optimize the if block and else block by pulling all of the common code out in front of the if statement, if at all possible
|
|
|
|
|
Look at this code:
DataTable dt = ds.Tables["Rest"]; ds = (DataSet) Session["ds"];
You are using ds before you assign a value to it.
---
b { font-weight: normal; }
|
|
|
|
|
D'oh! Okay, I get it to add a second line. (Small steps...small steps.)
New problem (imagine that!) When it adds the second line, it resets the first line. So anything I had previously entered is blanked out. How do I get it to retain what I already entered? I thought setting the dataset to the session did this.
Current code:
private void Button2_Click(object sender, System.EventArgs e)<br />
{ <br />
<br />
DataGridItemCollection items = dgParts.Items;<br />
<br />
<br />
int rows = items.Count;<br />
if (rows == 0)<br />
{<br />
<br />
BindData();<br />
DataTable dt = ds.Tables["Rest"];<br />
DataRow row;<br />
row = dt.NewRow();<br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
<br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();<br />
Session["test"]=ds;<br />
}<br />
<br />
else<br />
{<br />
ds = (DataSet)Session["test"];<br />
DataTable dt = ds.Tables[0];<br />
DataRow row;<br />
row = dt.NewRow(); <br />
strPartNumberInputReference = txtSearchPart.Text;<br />
row["PartNumber"] = strPartNumberInputReference; <br />
row["DrawingNumber"]=DBNull.Value;<br />
row["DrawingRevision"]=DBNull.Value;<br />
row["DwgPath"]=DBNull.Value;<br />
dt.Rows.Add(row); <br />
dt.AcceptChanges(); <br />
dgParts.DataSource=dt; <br />
dgParts.DataBind();<br />
}<br />
<br />
}
Thanks again for your help!
|
|
|
|
|
It could be that you're missing
DataTable dt = ds.Tables["Rest"];
in the block following the else statement.
|
|
|
|
|
A few days ago I posted a message wondering if anyone had any info on a problem with the ListView in .NET 2.0. The backcolor would disappear when scrolling or selecting and wouldnt come back until the form lost focus and got it back. (As a side note, calling Invalidate() or Refresh() on the ListView caused ALL items to lose color.) The solution turns out to be simple. In the Main method, just add Application.EnableVisualStyles(). HTH anyone with the same issue.
Aaron
|
|
|
|
|
I'm developing a plugin architecture for an application that will translate some database schema to an XML document that complies with an XML schema. Most of my architecture has been modelled after the article posted by 'Redth' here: http://www.codeproject.com/csharp/pluginsincsharp.asp
I'm able to call some methods from the host so long as those methods do not reference any classes outside of the namespace that the host sits in. Once I attempt to call methods from a namespace that it outside of the host (ie the namespace that contains all the data binding for the XML schema that I need to use) I get a MissingMethodException. All the correct references are in place and everything builds fine, yet the plugin can't access any of the classes in the data binding layer at runtime.
Any suggestions? Thanks!
|
|
|
|
|
Hi all,
I am planning to make a simple proxy server and i have been trying to retrieve the contents of a web page via Socket and send the display (NetworkStream.Flush) to a client browser. the html contents are being displayed properly but the images are not there. even the httpWebResponse.ContentLength shows the length of html content only.
someone plz tell me how to retrieve the images along with html using HttpWebRequest so that the page displays properly.
Thanks in advance.
|
|
|
|
|
If read through these forums you will see that re-asking the same question, even if it is re-phrased, doesn't help.
|
|
|
|
|
hi Mark,
actually, i has searched lots of forums, but i got nothing, i believe it is a quite common question, but i really canot find its answer.
if you know any relevant URL resources, plz tell me.
many thanks
|
|
|
|
|
Bluebamboo wrote: retrieve the images along with html
Mark is correct, besides HTTP does not work that way. Perhaps you should do some studying on the subject prior to attempting development?
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?" Colin Angus Mackay in the C# forum
led mike
|
|
|
|
|
sorry about the re-ask. but i really need help, and i am fastened on this problem long time~~
A question is really confusing me , how does a proxy program work that way when it responses the content of webpage with images to client explorer
|
|
|
|
|
I am not trying to be rude but HTTP is a fairly large protocol and using a forum is not the way to learn it. Go and study the HTTP protocol so that you "know" what you need to develop before you start writing code.
try Google: HTTP protocol tutorial
good luck
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?" Colin Angus Mackay in the C# forum
led mike
|
|
|
|
|
good suggetion for me
thank a lot
|
|
|
|
|
how to read tiff file content.
Roopesh Singh Gangwar
|
|
|
|
|
Have you tried FreeImage? It comes with a nice C# wrapper and seems to handle every TIFF type I've tried except JPEG 6.0 compression. If you need low-level stuff, you could write a wrapper for libtiff.
|
|
|
|
|
No i have not tried FreeImage. If you have code for that can u share it with me.
Roopesh Singh Gangwar
|
|
|
|
|
I guess this is pretty stupid question but ...
If I know the Tag (full path of the node), how can I use it in order of Checking the node.
Thanks
-- modified at 9:49 Thursday 6th July, 2006
|
|
|
|
|
Hello
Try to parse your tag & select them node by node, until you reach your target node.
Slow, but sure
Don't forget to put in mind that the tag may be in incorrect format
eg. -NOT A REAL CODE-
try
{
string MyTag = TheTag
Get firstNode Name
Select it from the root
remove its name from MyTag
while(MyTag.Length > 0)
{
Get NextNode Name
Select NextNode from the SelectedNode.Nodes
Remove its name from MyTag
}
}
catch
{
//error
}
//Now We got our node
Regards
|
|
|
|
|
I have a function in c++ returning array as:
int * funcArg(int *a ) {
int * DLLArg;
DLLArg=a;
return DLLArg;
};
I exported this function in dll( I checked with dumpbin.exe)
I declared this function in c# client as:
[DllImport("DenemeNonMfcDll.dll")]
public extern static int [] funcArg(int [] a);
And I called this function in c# client as:
int [] intArray;
intArray = new int[3] {0, 1, 2};
foreach (int num in funcArg(intArray))
{
System.Console.WriteLine(num.ToString());
}
But I got System.Runtime.InteropServices.MarshalDirectiveException and it says it can not marshal return value.
What should I do?
URGENT HELP!!!
|
|
|
|