Click here to Skip to main content
15,914,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello friends i have one problem now i working vb.net 2010. i connect the database with vb.net create a form, that form has been add, delete, save ... buttons but the save button i click the content is adding but it not view the current page i exit to the form and then reopen the form i see the data pls help how i view the data storing the same time.

VB
Private Sub btnsav_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsav.Click
      
        If txtcode.Text = "" Then
            MessageBox.Show("Enter the Material Code", "Code", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtcode.Focus()
            Exit Sub
        End If
        If txtname.Text = "" Then
            MessageBox.Show("Enter the Material Name", "Name", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtname.Focus()
            Exit Sub
        End If

        If txtunit.Text = "" Then
            MessageBox.Show("Enter the Unit", "Unit", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtunit.Focus()

            Exit Sub
        End If
        Try

            If Mode = "New" Then
              
                If MessageBox.Show("Are you sure to  store the values​​.!!!", "New Material", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then Exit Sub

                flag = cn.executeQuery(" insert into tbl_Mat_Master values ('" & txtcode.Text & "','" & txtname.Text & "','" & txtunit.Text & "') ")

                If flag = True Then
                    MessageBox.Show("Record Inserted Successfully", "Raw Material", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("Unable to Save Record", "Raw Material", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            ElseIf Mode = "Edit" Then
                If MessageBox.Show("Are You want to Edit the record", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then Exit Sub
                

                flag = cn.executeQuery(" UPDATE tbl_mat_master SET  Mat_Name= '" & txtname.Text & "', Unit = '" & txtunit.Text & "' WHERE Mat_Code= '" & txtcode.Text & "' ")

                If flag = True Then
                    MessageBox.Show("Record Updated Successfully", "Raw Material", MessageBoxButtons.OK, MessageBoxIcon.Information)
               
                End If
            End If
           

        Catch ex As Exception
            cn.DisplayErrorDialog(ex)
            cn.WriteToErrorLog(ex.Message, ex.StackTrace, "Error")

        End Try
    End Sub



it is my save button coding i use MSSql 2008 and Microsoft visual studio 2010
Posted
Updated 4-Feb-12 0:02am
v3
Comments
Sander Rossel 4-Feb-12 5:52am    
We can't help you with a question like that... What technology do you use? What have you tried? What does your code look like? What database are you using...
Member 8576839 4-Feb-12 5:57am    
Private Sub btnsav_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsav.Click

If txtcode.Text = "" Then
MessageBox.Show("Enter the Material Code", "Code", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtcode.Focus()
Exit Sub
End If
If txtname.Text = "" Then
MessageBox.Show("Enter the Material Name", "Name", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtname.Focus()
Exit Sub
End If

If txtunit.Text = "" Then
MessageBox.Show("Enter the Unit", "Unit", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtunit.Focus()

Exit Sub
End If
Try

If Mode = "New" Then

If MessageBox.Show("Are you sure to store the values​​.!!!", "New Material", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then Exit Sub

flag = cn.executeQuery(" insert into tbl_Mat_Master values ('" & txtcode.Text & "','" & txtname.Text & "','" & txtunit.Text & "') ")

If flag = True Then
MessageBox.Show("Record Inserted Successfully", "Raw Material", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Unable to Save Record", "Raw Material", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
ElseIf Mode = "Edit" Then
If MessageBox.Show("Are You want to Edit the record", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then Exit Sub


flag = cn.executeQuery(" UPDATE tbl_mat_master SET Mat_Name= '" & txtname.Text & "', Unit = '" & txtunit.Text & "' WHERE Mat_Code= '" & txtcode.Text & "' ")

If flag = True Then
MessageBox.Show("Record Updated Successfully", "Raw Material", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If
End If


Catch ex As Exception
cn.DisplayErrorDialog(ex)
cn.WriteToErrorLog(ex.Message, ex.StackTrace, "Error")

End Try
End Sub


it is my save button coding i use MSSql 2008 and Microsoft visual studio 2010
Sander Rossel 4-Feb-12 6:04am    
Please use the "Improve question" button next time. This time I did it for you.
RDBurmon 4-Feb-12 6:06am    
Where you want to show the data ?
I didn't see any discrepancies in save button.

1 solution

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:
VB
Public/Private Enum FormState
   None
   [New] ' Possibly rename this, like Append.
   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:
VB
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900