C# and SQL Autonumber
-
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.
jobDA.Fill(jobDS, "table_job"); DataRow job = jobDS.Tables["table_job"].NewRow(); job["CustomerID"] = cmb_cust.ValueMember; job["Date"] = date.Value; tandaterimaDS.Tables["table_job"].Rows.Add(job); jobDA.Update(jobDS, "table_job");
and these are the store procedure that I have: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); SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID) SET @JobID = SCOPE_IDENTITY()
Please kindly help me with this problem. I've been stuck here for several days. Thank you very much in advance. -
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.
jobDA.Fill(jobDS, "table_job"); DataRow job = jobDS.Tables["table_job"].NewRow(); job["CustomerID"] = cmb_cust.ValueMember; job["Date"] = date.Value; tandaterimaDS.Tables["table_job"].Rows.Add(job); jobDA.Update(jobDS, "table_job");
and these are the store procedure that I have: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); SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID) SET @JobID = SCOPE_IDENTITY()
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. WithNOCOUNT 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 yourNullReferenceException
. Most likely, though, I think theNullReferenceException
is happening because your original proc returns a 0-row recordset.
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
-
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.
jobDA.Fill(jobDS, "table_job"); DataRow job = jobDS.Tables["table_job"].NewRow(); job["CustomerID"] = cmb_cust.ValueMember; job["Date"] = date.Value; tandaterimaDS.Tables["table_job"].Rows.Add(job); jobDA.Update(jobDS, "table_job");
and these are the store procedure that I have: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); SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID) SET @JobID = SCOPE_IDENTITY()
Please kindly help me with this problem. I've been stuck here for several days. Thank you very much in advance.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 fromjobDS
, 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 -
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 fromjobDS
, 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 GnomeI'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 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
-
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:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace Program_Service { public class frm_tandaterima_baru : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; private System.Windows.Forms.Label lbl_customerID; private System.Windows.Forms.Label lbl_description; private System.Windows.Forms.TextBox txt_description; private System.Windows.Forms.ComboBox cmb_cust; private System.Windows.Forms.Button btn_save; private System.Windows.Forms.Button btn_newjob; private System.Windows.Forms.Button btn_exit; private System.Data.SqlClient.SqlDataAdapter jobDA; private System.Data.SqlClient.SqlCommand select_job; private System.Data.SqlClient.SqlCommand update_job; private System.Data.SqlClient.SqlCommand insert_job; private System.Data.SqlClient.SqlCommand delete_job; private Program_Service.ds_parent jobDS; private System.Data.SqlClient.SqlDataAdapter custDA; private System.Data.SqlClient.SqlCommand sqlSelectCommand1; private System.Data.SqlClient.SqlCommand sqlInsertCommand1; private System.Data.SqlClient.SqlCommand sqlUpdateCommand1; private System.Data.SqlClient.SqlCommand sqlDeleteCommand1; public frm_newjob() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void frm_newjob_Activated(object sender, System.EventArgs e) { // Fill DataSet with all data needed custDA.Fill(jobDS, "table_cust"); // Set the focus on activated this.cmb_cust.Focus(); } private void btn_save_Click(object sender, System.EventArgs e) { jobDA.Fill(jobDS, "table_job"); DataRow job = jobDS.Tables
-
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:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace Program_Service { public class frm_tandaterima_baru : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; private System.Windows.Forms.Label lbl_customerID; private System.Windows.Forms.Label lbl_description; private System.Windows.Forms.TextBox txt_description; private System.Windows.Forms.ComboBox cmb_cust; private System.Windows.Forms.Button btn_save; private System.Windows.Forms.Button btn_newjob; private System.Windows.Forms.Button btn_exit; private System.Data.SqlClient.SqlDataAdapter jobDA; private System.Data.SqlClient.SqlCommand select_job; private System.Data.SqlClient.SqlCommand update_job; private System.Data.SqlClient.SqlCommand insert_job; private System.Data.SqlClient.SqlCommand delete_job; private Program_Service.ds_parent jobDS; private System.Data.SqlClient.SqlDataAdapter custDA; private System.Data.SqlClient.SqlCommand sqlSelectCommand1; private System.Data.SqlClient.SqlCommand sqlInsertCommand1; private System.Data.SqlClient.SqlCommand sqlUpdateCommand1; private System.Data.SqlClient.SqlCommand sqlDeleteCommand1; public frm_newjob() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void frm_newjob_Activated(object sender, System.EventArgs e) { // Fill DataSet with all data needed custDA.Fill(jobDS, "table_cust"); // Set the focus on activated this.cmb_cust.Focus(); } private void btn_save_Click(object sender, System.EventArgs e) { jobDA.Fill(jobDS, "table_job"); DataRow job = jobDS.Tables
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
-
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:
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); SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID) SET @JobID = SCOPE_IDENTITY()
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. -
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:
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); SELECT JobID, CustomerID, Date FROM dbo.tabel_job WHERE (JobID = @JobID) SET @JobID = SCOPE_IDENTITY()
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
-
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.