|
Your save routine is still missing the call to cmd.ExecuteNonQuery() , which should be just before the _cn.Close() line. Without it, the record won't be saved.
Your remove routine always deletes the record where code = 1 ; since the command text doesn't reference the @code parameter, the value you've set is never used. It also deletes the record before asking the user whether they want to delete the record, making the question meaningless.
You might also want to look at wrapping the connection and command objects in a Using block[^] to make sure they get cleaned up properly even if there's an exception:
Using con As New SqlConnection(strconnection)
Using cmd As SqlCommand = con.CreateCommand()
...
End Using
End Using
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
SAVE BUTTON
-------------
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
Dim strconnection As String = "Data Source=ELIANE-VAIO\SQLEXPRESS;Initial Catalog=SchoolDB;Integrated Security=True;"
Dim _cn As SqlConnection = New SqlConnection(strconnection)
Dim cmd As New SqlCommand
_cn.Open()
cmd.CommandText = "INSERT INTO StudentInfo([Code], [Full-Name], [Position], [Title], [Phone-Number], [Address], [Date-Time]) VALUES (<a href="/Members/code">@Code</a>, <a href="/Members/Name">@Name</a>, <a href="/Members/position">@Position</a>, <a href="/Members/title">@Title</a>, @PhoneNumber, <a href="/Members/address">@Address</a>, <a href="/Members/datetime">@DateTime</a>);"
cmd.Connection = _cn
cmd.Parameters.AddWithValue("<a href="/Members/code">@Code</a>", Val(Me.t1.Text))
cmd.Parameters.AddWithValue("<a href="/Members/Name">@Name</a>", Me.t2.Text)
cmd.Parameters.AddWithValue("<a href="/Members/position">@Position</a>", Me.c1.Text)
cmd.Parameters.AddWithValue("<a href="/Members/title">@Title</a>", Me.c2.Text)
cmd.Parameters.AddWithValue("@PhoneNumber", Me.t5.Text)
cmd.Parameters.AddWithValue("<a href="/Members/address">@Address</a>", Me.t6.Text)
cmd.Parameters.AddWithValue("<a href="/Members/datetime">@DateTime</a>", Me.t7.Value)
cmd.ExecuteNonQuery()
_cn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
REMOVE BUTTON
-------------
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
Me.StudentInfoBindingSource.RemoveCurrent()
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
Dim strconnection As String = "Data Source=ELIANE-VAIO\SQLEXPRESS;Initial Catalog=SchoolDB;Integrated Security=True;"
Dim _cn As SqlConnection = New SqlConnection(strconnection)
_cn.Open()
cmd.Connection = _cn
cmd.CommandText = "Delete From studentInfo where code= " & Val(t1.Text)
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
Else
cmd.ExecuteNonQuery()
End If
Catch ex As Exception
MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")
Finally
con.Close()
End Try
End Sub
|
|
|
|
|
Almost perfect!
For completeness, I'd be inclined to use a parameter in the remove routine as well:
cmd.CommandText = "delete from StudentInfo where Code = @Code"
cmd.Parameters.AddWithValue("@Code", t1.Text)
The Val function[^] converts the text to a number, so you're not going to get SQL Injection from that. However, if you get in the habit of always using parameters, you're less likely to accidentally introduce SQLi in the future.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Okay. Thank You
|
|
|
|
|
Member 10388494 wrote: Here is one of them. That "many ways don't work" means little; I'd be more interested in what you mean if you say that *this* version doesn't work -
Does it throw an exception? Does it insert the record or not?
Member 10388494 wrote: Violation of PRIMARY KEY constraint 'PK_StudentInfo'. Cannot insert duplicate key in object 'dbo.StudentInfo'. Sounds like you're inserting the same record twice.
Member 10388494 wrote: <layer>but i am sure 100% that there is no record that have the same code. What fields are named in "PK_StudentInfo"? How are you sure that the record isn't in there? Can you delete everything in there and see if it saves the record once?
How does your update-statement look?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The reason that it isn't working is because you have created a primary key in your table, but because you haven't actually specified a value for it, you are attempting to insert 0 into it (assuming it's of type int). What you need to do is open up the table definition and convert this field into an identity column, and let the database take care of this for you. Alternatively, you can be really cool and convert that column to a type that supports Guids, and assign your own unique value in code.
|
|
|
|
|
Hello. how can i store a radiobutton in sql?
i have RDFemale fro a female and RDMale fro a male.
i tried this:
_cn.Open()
Dim sqlstring As String = "Insert into Student values('" & txtCode.Text & "', '" & txtName.Text & "', '" & RDFemale.value & "', '" & cmbPosition.Text & "','" & cmbTitle.Text & "', '" & txtPhoneNumber.Text & "', '" & txtAddress.Text & "','" & DateTimePicker1.Value & "',' " & cmbPmode.Text & "', '" & txtRemarks.Text & "');"
Dim _cmd As New SqlCommand(sqlstring, _cn)
_cmd.ExecuteNonQuery()
_cn.Close()
MsgBox("Record saved!!", MsgBoxStyle.Information, "Successfully saved")
End Sub
|
|
|
|
|
You can't store it directly. Since you are referring to a RDMale and a RDFemale I'd recommend you to store it as an enum.
This enum may look like that:
enum Gender{
Male,
Female
};
Afterwards you are able to store the value into a database like a normal enum, you only have to parse it to a numeric value before you store it.
|
|
|
|
|
You would store the result of the radio button as either an int or a bit (assuming that you are using SQL Server).
i.e.
dim Male as boolean = RDMale.checked
if you test that line in your application you will see that it will return true if you have checked that button or false if you haven't
Also I would have a look at using SQLParameters as this will save you from SQL Injection have a look at this StackOverflow question the actual question will show you how to set it up in VB.NET
http://stackoverflow.com/questions/16765837/set-sqlparameter-in-vb-net[^]
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
Let me Explain about the entire structure of what we are doing before start ask the question
Our client required Custom Primary keys for all our transactions. To create the key, we are using store procedure which require an input parameter named "SiteCode". This store procedure will create sequence keys for every transaction in the application we have made
Every transaction insert we have written a Stored procedure and inside this sp's we have call the key generating SP before we do insert statements and we have given those key as a primary key to the insert statement. So the actual "SiteCode" input parameter has passed to every transaction SP's as an input parameter
We have created DB first entity Model and all the entity class has been created
Now We have a screen in WPF which capture the data for the models Customer, Address, Contact information.
so Instead of getting model one by one and do save changes. we would like to map the stored procedure function (used to insert the values) with respective model
for example CutomerRef attribute to sp_CreateCustomer SP in entity framework
While we are doing this mapping we can mapp all the fields except the "SiteCode" field which don't have reference in any entity model.
We have tried to create partial class to the cutomerRef model and add this property in to that partial class and try to map with SP it doesn't work Also, tried to create the field in the model itself in entity model and map with the sp parameters also not working.
We know, we can do this to call sps from stored procedure but we need them to do just by Context.Customer.Add(customerModel)
Any help would be appriciated
Regards,
Ansari
|
|
|
|
|
Hi,
I know that this question also relates to Excel but I wanted to know if there's a way or if somebody can direct me how can I access a PowerPivot table in Excel and manipulate it i.e. update data, save data, retrieve data etc'.
Basically my purpose is to interact with the PowerPivot table so I can display the data and update it from a web browser.
Avi
|
|
|
|
|
Try and give this[^] a go.
|
|
|
|
|
Thanks! It looks good.
I'll check it out.
|
|
|
|
|
i wanna new features about 2008,2010 and 2012 its urgent please ...........mail me vsateesh.mca@gmail.com or call me 9014470340
Thanks And Regards
v sateesh
9014470340
|
|
|
|
|
|
And you like spam, don't you?
Now everyone knows your email address, and even your phone number. What a good invitation to spammers!
|
|
|
|
|
There will be tons of information available on msdn and on the internet.
No one but spammers will respond to you if you give your email id out on a public forum.
|
|
|
|
|
|
SateeshVankayala wrote: urgent please
Urgent for you, not to anyone else.
And you seriously think people will draft an email or call you to provide you with the information you need? If this is your attitude towards learning, I'm sure you wouldn't learn anything in your life.
|
|
|
|
|
In keypress event the character which i press should come after my input which i send using send keys.
here is the code
if asc(e.keychar)=102 then 'ascii value of "f"
system.windows.forms.sendkeys.send("A")
end if
"f" should come after "A",but it is displaying as "fA"
How to solve this guys....
|
|
|
|
|
|
You can set the SuppressKeyPress property of the KeyEventArgs supplied in your event handler to true and then pass on both A and F in the SendKeys.
|
|
|
|
|
Hi,
I have created a custom addIn for outlook 2007 using VS2010.
I have created a custom Ribbon using xml containing a single toggleButton control. This is for new message screen. After clicking the "New Mail Message" from outlook custom ribbon visible along with the toggle button. Once I pressed the toggle button and sent the mail the toggle button remains same (on press) for every new message later. Can anybody please help me how to make toggleButton getPressed status to unchecked for every new mail message click event.
Thanks.
|
|
|
|
|
Why are you even using a toggle button? Why not a normal button??
|
|
|
|
|
byte b1 = 4;
byte b2 = 2;
byte result = b1 - b2 //error here Cannot explicitly convert type 'int' to 'byte'. Are you missing a cast?
Question is : where does the compiler see the 'int' here, where all variables are byte?
|
|
|
|