-
Apr 15th, 2018, 12:38 PM
#1
Thread Starter
Hyperactive Member
Error enregistration record in the second time
Hello VBForums
Hello every one
Please if you can help me to resolve this problem
In my Form1 i have Button1 to empty all TextBox and for add an automatic number to TextBox1 .. like this :
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Command As New SqlCommand("SELECT max(IDNUMBER) FROM [ROOMBADGE]", Conn)
If Conn.State = ConnectionState.Closed Then Conn.Open()
If Command.ExecuteScalar Is DBNull.Value Then
TextBox1.Text = 1
TextBox2.Focus()
Else
TextBox1.Text = Command.ExecuteScalar().ToString() + 1
TextBox2.Focus()
End Sub
In Button2 for save .. code for check if the number is already exist and save in DataBase :
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Verify As New SqlCommand With {.Connection = Conn, .CommandText = "SELECT [IDNUMBER] FROM [ROOMBADGE] WHERE [IDNUMBER] = @IDNUMBER "}
Verify.Parameters.Add("@IDNUMBER", SqlDbType.Int).Value = Convert.ToInt32(TextBox1.Text.Trim)
Insert INTO ..
.Parameters.Add("@IDNUMBER", SqlDbType.Decimal, 18).Value = Decimal.Parse(TextBox1.Text)
End Sub
All works very very well and I have not any problem
I want to add the year with auto number in TextBox1 ..like this :
1-2018
2-2018
3-2018
So .. i change concerned lines like that and i change type colums in DataBase to Nvarchar 50 :
Code:
TextBox1.Text = "1" & "-" & Format(Date.Now, "yyyy")
TextBox1.Text = Command.ExecuteScalar().ToString() + 1 & "-" & Format(Date.Now, "yyyy")
Verify.Parameters.Add("@IDNUMBER", SqlDbType.NVarChar).Value = TextBox1.Text.Trim
.Parameters.Add("@IDNUMBER", SqlDbType.NVarChar).Value = TextBox1.Text
The result ..
The first time all goes well and I can save when DataBase is empty
But when i click Button2 for save in second time .. i have an error in this Line
Code:
TextBox1.Text = Command.ExecuteScalar().ToString() + 1 & "-" & Format(Date.Now, "yyyy")
Message error ( Convertion from string "1-2018" to type double is not valid )
Thank you in advance for help
Cordially
MADA
Last edited by MADA BLACK; Apr 15th, 2018 at 12:42 PM.
-
Apr 15th, 2018, 06:29 PM
#2
Re: Error enregistration record in the second time
Presumably the issue is here:
vb.net Code:
Command.ExecuteScalar().ToString() + 1
If ExecuteScalar returns "1-2018" (which you should know, because you should have actually looked) then obviously that's not a number so how does adding 1 to it make sense? It seems like the logical thing to do would be to call Date.ParseExact to convert it to a Date and then call AddMonths on that. You can then call ToString on the result to convert it back to a String in the desired format.
-
Apr 16th, 2018, 03:12 AM
#3
Thread Starter
Hyperactive Member
Re: Error enregistration record in the second time
Thank you jmcilhinney for the explanation
Please why do we change to the month because the numbers will be like that :
1-2018,2-2018,3-2018,4-2018.......254-2018,76589-2018
I tried to call DateParse but could not succeed
Cordially
MADA
-
Apr 16th, 2018, 05:31 AM
#4
Re: Error enregistration record in the second time
Originally Posted by MADA BLACK
I tried to call DateParse but could not succeed
Then you did it wrong. If you don't show us what you did then we can't tell you what's wrong with it. That said, I told you to use Date.ParseExact, not DateParse. I don't know what that is. Date.ParseExact would be called in much the way that the examples already on the web demonstrate and the documentation for the method explains.
-
Apr 17th, 2018, 08:31 AM
#5
Re: Error enregistration record in the second time
Originally Posted by jmcilhinney
Then you did it wrong.
He may have done it wrong but ParseExact and AddMonths is not what he needs since the overall value doesn't represent a date. His OP and #3 both indicate this is an autonumber field where he simply has appended the year (e.g., 37429-2018).
As such, I think jmc can come up with some whizbang way to do this. My old fashioned way would be to just strip out the autonumber, convert to integer, increment, convert back to string and then append the remainder of the original string back on.
Code:
Dim tempStr As String = Command.ExecuteScalar().ToString()
Dim tempDash As Integer = InStr(tempStr, "-")
Dim newString As String = (CInt(tempStr.Substring(0, tempDash - 1)) + 1).ToString & "-" & tempStr.Substring(tempDash)
-
Apr 17th, 2018, 06:07 PM
#6
Re: Error enregistration record in the second time
Originally Posted by topshot
He may have done it wrong but ParseExact and AddMonths is not what he needs since the overall value doesn't represent a date.
Ah, I misunderstood that to be month and year and never realised my mistake. Sorry for the misinformation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|