As I still don't really get your problem I can't begin with all the horrors I see in this code.
One is the use of Strings where you clearly should've used an Enum... Something like:
Public/Private Enum FormState
None
[New]
Edit
...?
End Enum
Second, and I see this waaaay to often, you are not
parameterizing your queries[
^]! If a user would enter the value "D'artagnan" your query would break and you software would throw an Exception. Also, harmful attacks can be made! This is called
SQL Injection[
^].
Use parameterized queries like follows:
Dim cmd As New DbCommand("insert into tbl_Mat_Master values ('@Code','@Name','@Unit') ")
cmd.Parameters.AddWithValue("@Code", txtCode.Text)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Unit", txtUnit.Text)
cmd.ExecuteNonQuery
This will prevent your query from breaking, will make your software a lot more secure, will make it more likely for your database to re-use your query (improving performance) etc.
All the uses of
Exit Sub
make me cry a little inside too. If any programmer other than you would ever have to debug your code they'd wonder why the code never hits their breakpoint. Well, it's just your code jumping in and out of code at any random line...
Besides that your question still isn't clear. Where does
cn
come from? Where is this code called? What happens after the code is called?
I don't wish to be rude, but perhaps you should start reading a book on programming before continueing any further... Both your question and code show you have little clue as to what you're doing. I hope your not feeling attacked or demotivated by this post. I say these things to help you!
Good luck.