Dear Freinds

I have a few question Regarding VB.NET 1) I have a datgridview and i want when user input the datagrid 3rd cell the 4 cell of datagrid readonly = false means user not input value in 4th cell 2)i want a autonumber generate in textbox is alphanumeric like "vendor9" or " "vendor10" bt when i go to save this it give error of "double to type string is not valid" i have use a following coding

Dim newac As Double
 Dim mydata = From v In voucher.vr_masts Select v
 For Each vr_mast In mydata
 newac = vr_mast.vr_no
 Next
 newac = newac + 1
 Select ComboBox1.SelectedIndex
 Case 0
 TextBox1.Text = "VR" & newac
 Case 1
 TextBox1.Text = "CUST" & newac
 End Select

Thanks

At which line error is occuring ???

Hi!

Change line 4 like this:

newac = CDbl(vr_mast.vr_no)

i.e., convert vr_no to type double.

no its not working

it still give this error


"Conversion from string "CUST9" to type 'Double' is not valid."

hmmm...

"vr_no" is alphanumeric and you are trying to cast it into Double which is not possible. you need to take a string variable instead of Double to hold "vr_no".

like:

dim vr_num as string = CDbl(vr_mast.vr_no)

chk my new coding and correct me where i am wrong because it give error

Dim newac As Integer
        Dim cust As String = "cust"
        Dim vend As String = "vend"

        Dim mydata = From v In voucher.vr_masts Select v
        For Each vr_mast In mydata
            Dim vr_nos As String = CDbl(vr_mast.vr_no)
            newac = vr_nos

        Next
        newac = newac + 1
        TextBox1.Text = newac

Check this i think it will help regarding your 1st problem

Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
        If e.ColumnIndex = 2 Then
            DataGridView1.Columns(3).ReadOnly = False
        End If
    End Sub

Best of Luck

@prvnkmr194

can u more clarify your code
bcz it nt working properly

What does this line mean ???

newac = newac + 1

are you willing to increment "vr_no" by 1, like CUST9 to CUST10 ???
because without knowing your intentions it is impossible to modify your code.

Yes I am willing to increment "vr_no" by 1, like CUST9 to CUST10

So ,try like this change line 4 to:

newac = CInt(vr_no.Substring(4))

This will take "9" from "CUST9" and now:

newac = newac + 1

will increment it to 10

then your this code:

Select ComboBox1.SelectedIndex

Case 0

TextBox1.Text = "VR" & newac

Case 1

TextBox1.Text = "CUST" & newac

End Select

will make TextBox1.Text either CUST10 or VR10 depending on condition.

Now It gve this error

"startIndex cannot be larger than length of string. Parameter name: startIndex"

Could you plz tell me what is "Substring(4)"?

Okay, I got the problem.

First "Substring(4)", it extracts the string from index 4 to the end of string.
(Note: index is zero based, i.e., starts from zero)

The error is so because your sample can have "VR9" as well as "CUST9"
so in case of "VR" extraction should be start with index 2, and in case of "Cust9" extraction should be start from index 4.

so your code should be like this:

Dim newac As Double
 Dim mydata = From v In voucher.vr_masts Select v
 For Each vr_mast In mydata
        If (vr_mast.vr_no.StartsWith("VR")) Then
            newac = CInt(vr_mast.vr_no.Substring(2))
        Else
            newac = CInt(vr_mast.vr_no.Substring(4)) 
        End If
 Next
 newac = newac + 1
 Select ComboBox1.SelectedIndex
 Case 0
 TextBox1.Text = "VR" & newac
 Case 1
 TextBox1.Text = "CUST" & newac
 End Select

Freind


iT gves again error of "String TO type integer is not valid"

Dim vr_nos As String = CDbl(vr_mast.vr_no)
That will still cause a problem becos you are still trying to convert a string to a double. So leave out the cdbl and just pull in the full string:
Dim vr_nos As String = vr_mast.vr_no
So you have, say, 'cust9' or 'vend9'
Then, assuming it is ALWAYS 'cust' or 'vend', AKA 4 text characters long:
Dim ContactType as string = vr_nos.substring(0,3)
Dim ContactID as string = vr_nos.replace(ContactType ,"")

This will leave you with 'vend' or 'cust' in ContactType and the number in ContactID
Then:
if isnumeric(ContactID) then newac = cdbl(ContactID) + 1
Then:
ContactType & newac
Will produce the new account number you want.

This is a very clumsy way of doing things, you should have the contact type and ID number stored separately in the voucher structure.

Replace your for loop with this:

For Each vr_mast In mydata
            Dim tmp As String = System.Text.RegularExpressions.Regex.Replace(vr_mast.vr_no, "(?i)[a-z]*", "")
            newac = CDbl(tmp)
        Next
       newac = newac + 1

Here, this is a pattern "(?i)[a-z]*" which finds alphabets in "vr_mast.vr_no" and "Replace()" methods replaces those alphabets with "Empty String (i.e., "")"

(?i) ignore case of characters
[a-z] search includes alphabets from a-z
* zero or more times

Using this method you only have a number in "newac" which can easily be incremented.

@ShahanDev

Thank you sir your coding now work properly thank you so much

will you give me your msn or yahoo id so i will keep in touch in with you

my 2nd problem has been solved now plz anyone can solve my first problem

Post the link of your first problem and close this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.