|
Hi,
I am a newbie in C# and SQL programming, and I have questions regarding autonumber for one of my table coloumn. Please kindly guide me.
I have this one table consisting of JobID, CustomerID, and Date.
On the coloumn JobID, I like to put autonumber there, since JobID coloumn is also my primary key for the table, so null is not allowed.
I first build the Data Adapter, by using design view, and make new stored procedure for that table.
I also changed the property on the column by setting the Identity to YES, Identity Seed to 1, and Identity Increment to 1.
I did run the SQL stored procedure, it gave me the autonumber on coloumn JobID, but when I tried to put the command in my windows form, it game me error: "An unhandled exception of type 'System.NullReferenceException' Occured"
These following are the codes that I put on my windows form.
<br />
jobDA.Fill(jobDS, "table_job");<br />
<br />
DataRow job = jobDS.Tables["table_job"].NewRow();<br />
job["CustomerID"] = cmb_cust.ValueMember;<br />
job["Date"] = date.Value;<br />
tandaterimaDS.Tables["table_job"].Rows.Add(job);<br />
jobDA.Update(jobDS, "table_job");<br />
and these are the store procedure that I have:
<br />
ALTER PROCEDURE programservice.insert_job<br />
(<br />
@JobID int output,<br />
@CustomerID char(20),<br />
@Date datetime<br />
)<br />
AS<br />
SET NOCOUNT OFF;<br />
INSERT INTO dbo.table_job(CustomerID, Date) VALUES (@CustomerID, @Date);<br />
SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID)<br />
SET @JobID = SCOPE_IDENTITY()<br />
Please kindly help me with this problem. I've been stuck here for several days.
Thank you very much in advance.
|
|
|
|
|
Swap the last two lines, like so:
ALTER PROCEDURE programservice.insert_job
(
@JobID int output,
@CustomerID char(20),
@Date datetime
)
AS
SET NOCOUNT OFF
INSERT INTO dbo.table_job(CustomerID, Date) VALUES (@CustomerID, @Date);
SET @JobID = SCOPE_IDENTITY()
SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID)
It's also probably a good idea to SET NOCOUNT ON , instead of setting it off. With NOCOUNT OFF , SQL Server will send a message back to the client like "1 Rows affected." Some versions of ADO/MDAC will receive this message and generate a null recordset from it. This may be causing your NullReferenceException . Most likely, though, I think the NullReferenceException is happening because your original proc returns a 0-row recordset.
Grim (aka Toby) MCDBA, MCSD, MCP+SB
|
|
|
|
|
You didn't say what line the exception happened on. Also, a more complete code sample would be needed. The sample you provided doesn't have a definition for tandaterimaDS . It looks like your getting a new blank row from jobDS , filling it, then trying to add that row to a different dataset, tandaterimaDS . This won't work. The you created must be added back to the dataset that created it.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I've tried to set the nocount to on (SET NOCOUNT ON) and change the little mistake I have (JobDS), but it still come out in error.
When the error comes out, it highlight this line:
tandaterimaDS.Tables["table_job"].Rows.Add(job);
I am so confused with this problem, because when I run the stored procedure only, it has no problem or error.
|
|
|
|
|
I've already asked this question, but I'll do it again...
The stored procedure probably isn't the problem. Please post a more complete code sample so we can see what your doing.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
The complete code will be very long, but let me explain and send you more detail code.
What I am trying to do is make a form for data input, so the user will select customer from the combo box, and fill the textbox for description of the job. For the date, it will automatically entered.
When the user hit save button, it will create new JobID, and I was hoping to get it print out, but not yet there.
This is the more complete or detail code:
<br />
using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Data;<br />
using System.Data.SqlClient;<br />
<br />
namespace Program_Service<br />
{<br />
<br />
public class frm_tandaterima_baru : System.Windows.Forms.Form<br />
{<br />
private System.ComponentModel.Container components = null;<br />
private System.Windows.Forms.Label lbl_customerID;<br />
private System.Windows.Forms.Label lbl_description;<br />
private System.Windows.Forms.TextBox txt_description;<br />
private System.Windows.Forms.ComboBox cmb_cust;<br />
private System.Windows.Forms.Button btn_save;<br />
private System.Windows.Forms.Button btn_newjob;<br />
private System.Windows.Forms.Button btn_exit;<br />
private System.Data.SqlClient.SqlDataAdapter jobDA;<br />
private System.Data.SqlClient.SqlCommand select_job;<br />
private System.Data.SqlClient.SqlCommand update_job;<br />
private System.Data.SqlClient.SqlCommand insert_job;<br />
private System.Data.SqlClient.SqlCommand delete_job;<br />
private Program_Service.ds_parent jobDS;<br />
private System.Data.SqlClient.SqlDataAdapter custDA;<br />
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;<br />
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;<br />
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;<br />
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;<br />
<br />
public frm_newjob()<br />
{<br />
InitializeComponent();<br />
}<br />
protected override void Dispose( bool disposing )<br />
{<br />
if( disposing )<br />
{<br />
if(components != null)<br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
private void frm_newjob_Activated(object sender, System.EventArgs e)<br />
{<br />
custDA.Fill(jobDS, "table_cust");<br />
this.cmb_cust.Focus();<br />
}<br />
private void btn_save_Click(object sender, System.EventArgs e)<br />
{<br />
jobDA.Fill(jobDS, "table_job");<br />
<br />
DataRow job = jobDS.Tables["table_job"].NewRow();<br />
job["CustomerID"] = cmb_cust.ValueMember;<br />
job["Date"] = date.Value;<br />
job["Description"] = txt_description.Text;<br />
jobDS.Tables["table_job"].Rows.Add(job);<br />
jobDA.Update(jobDS, "table_job");<br />
}<br />
<br />
private void btn_newjob_Click(object sender, System.EventArgs e)<br />
{<br />
this.txt_description.Clear();<br />
}<br />
<br />
private void btn_exit_Click(object sender, System.EventArgs e)<br />
{<br />
this.Close();<br />
}<br />
}<br />
}<br />
All of the component I had in the form are being put by using designer mode.
One change that I've made on the jobDS.xsd is that the column jobID: nillable = true, NullValue = (Null), ReadOnly = True.
Please advice what should I do, and thank you very much for your attention and help.
|
|
|
|
|
From what I can piece together from your submissions, your calling the Update method on the dataadapter, but using an INSERT SQL statement on that command. If your using an INSERT statement, are you also running a SELECT command after the INSERT so that dataset gets updated and you receive the new record with it's jobID? The jobID won't get created until the new data is commited to the SQL database.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I thought I already have SELECT Command after INSERT on the stored procedure.
Do I need to add another one?
This is my stored procedure for the insert command:
<br />
ALTER PROCEDURE programservice.insert_job<br />
(<br />
@JobID int output,<br />
@CustomerID char(20),<br />
@Date datetime<br />
)<br />
AS<br />
SET NOCOUNT OFF;<br />
INSERT INTO dbo.table_job(CustomerID, Date) VALUES (@CustomerID, @Date);<br />
SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID)<br />
SET @JobID = SCOPE_IDENTITY()<br />
I still don't understand why can't I set null to the JobID dataset, since I already set the niilable properties to TRUE.
Is it possible to update or insert the data without the usage of Dataset? Since this is stored procedure, and I believe that the process took place on the database / Server.
Also, is there another way of creating autonumber ID like in this case?
The Database are very likely to be processed by more than one client at the same time.
|
|
|
|
|
Like the first responder said in your post, you SQL statement IS wrong. You said you corrected it, but you changed it back to the incorrect statement again:
ALTER PROCEDURE programservice.insert_job(
@JobID int output,
@CustomerID char(20),
@Date datetime
) AS
SET NOCOUNT ON
INSERT INTO dbo.table_job(CustomerID, Date) VALUES (@CustomerID, @Date);
SET @JobID = SCOPE_IDENTITY()
SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID)
The JobID will not have a number until SQL commits the new record to the database (the INSERT statement). Then you can get the new JobID into @JobID using SCOPE_IDENTITY(). Then the SELECT statement returns the updated record to the caller as the sole record in the dataset.
Since JobID is the Primary Key and IDENTITY, it CAN'T ever be NULL...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I figured out the problem already. It was the dataset.
I tried to delete and start from beginning again, create the dataadapter, but this time, I did not generate dataset.
So, First I delete the table from SQL Server, and then create new one with the same name.
I change the property of the table in design mode, Identity = Yes, Identity Seed = 1, and Identity Increment= 1.
And then create Dataadapter in design mode, without generating dataset.
I ran the compiler, and it worked fine.
I guess what I did previously was correct but probably messed up in the middle.
Nevertheless, thank you very much Dave. You explained to me how the autonumber works.
|
|
|
|
|
In a M*N Rectangle, i want to Highlight K points .
The K points must be well-distributed.
How to do it ?
|
|
|
|
|
Well, you'll just have to use a well-distributed random number generator to generate coordinate pairs for the points. System.Random should be fine.
|
|
|
|
|
Hi,
I received from a customer an SNMP MIB file for remote a device with TCP/IP.
I looking some help,link or sample for use it.
Best Regards
youssef
|
|
|
|
|
Why did the customer send you this file? What do you want to do with the information it provides?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I received this file for controlling a device and received some alarms.
I received in the same time a serial protocol but it is so slowly for received the all alarms and set some parameters.
I have 50 devices to control !!!! in the same time.
Best regards
youssef
|
|
|
|
|
The MIB file usually get imported into some management application like HP OpenView. It describes to that application what the device is, it's properties, controls, alarms, ...
Are you writing your own management application?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
yes, if you want.
I have been developed an automation that controls many device for telecoms with some GUI.
Since always, I used the protocol series for the communication but recently, Ethernet connection is available and allows more control that a serialport(depends on speed d'exécution, the lenght of the datas) and allows also a profit in serialport connections with the servers connected to the system.
Best Regards
youssef
|
|
|
|
|
I'm looking to try and dock two Forms together side by side, basically each form can have its own dimensions. But they should move together when dragged. Can I get a shove in the right direction from anyone ?
|
|
|
|
|
Extend Form and override OnLocationChanged . Also add a property to the class of type Form that you set the Form to be docked.
In your implementation, get the Location of each Form (don't forget to check for null before accessing the other Form ). Some simple math will tell you which side the other Form is in relation to this . If the Left + Width of the other form is, for example, within a certain threshhold of your current Form , then adjust the Location * of the other Form in relation to your current Form.Location .
* Don't forget that since Location (a Point ) is a value type, you must create a new Point and set the Location . Setting Form.Location.X and/or Form.Location.Y will not move the other Form .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Great info Heath, thanks once again...
One last follow-up....how should I go about handling the z-order of the two forms, will one always be active and the other not ? Is it possible to have both windows active at the same time ? Didn't know if you had any tricks up your sleave for making both Forms active at the same time...If not then I think I have all the info I need...
Thanks Again
|
|
|
|
|
Only 1 form can be active at any one time. You could set the z-zorder of one of the forms to be on top and the other form to be directly 'behind' it in the z-order. When you switch the active forms, you can switch the zorder so the form you just left is directly behind the form you just activated.
This may seem stupid to implement because the form that loses focus just drops to the #2 place behind the form/app that gets the focus, but it makes more sense when you click on a different app to bring it to the top of the z-order, then click one of your forms. I think your find that the z-order will become your active form at the top, then the application you just left, the your second form in the #3 spot.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I figured as much, thanks for your reply too.
|
|
|
|
|
Also, if one of these forms acts as the "main" form, you could use Form.AddOwnedForm and pass your "child" forms. This way, each form can be active (only 1 at a time) but they will have a z-order in relation to other processes. So all your applications forms will be over or under other processes. Owned forms will also overlap the "main" form and will be closed when your "main" form is closed.
See the .NET Framework SDK documentation for Form.AddOwnedForm for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Well I've implemented the changes you have suggested and the docking form works fine. However when the form is enlarged and then the large window is moved the movement becomes very jerky. I'm wondering if the movement of both forms is too much for the single thread. So now I'm attempting to multi-thread the app. I'd like to create the main form in one thread and the docked form in a second. However I'm running into a logistical problem where the second thread that creates the Docking form goes out of scope and terminates.
The following code is where I create the second thread.
private void FavWinThread()
{
this.ShowFavWin=true;
frmFav = new frmFavorites(this);
while(!frmFav.WinReady)
Thread.Sleep(10);
frmFav.Show();
}
if(frmFav==null)
{
FavThread = new Thread(new ThreadStart(FavWinThread));
FavThread.Start();
}
I assume I'm creating the thread in the wrong place or I'm not maintaining the thread correctly ? Perhaps someone could give me a suggestion ? Any insight would be appreciated.
|
|
|
|
|
First, you need to make sure that the variable does not go out of scope and that you have a message pump attached to the Form , otherwise it will be closed when the thread terminates (FYI, Application.Run does this with your main form). The easiest way is to call Form.ShowDialog , but don't forget to dispose the form (i.e., call Dispose ) when ShowDialog returns and (if you need to) you get all information from it before disposing it. There are other ways, and reading through the System.Windows.Forms namespace in the .NET Framework SDK at some of the more obscure classes should help if you have experience with native Win32 programming with Windows APIs.
One thing to keep in mind is that you will need to use Control.Invoke to communicate between to controls (i.e., Form s) created on different threads. Also, controls created on different threads cannot be hosted in the same container (which doesn't apply here, but I thought I'd mention it anyway).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|