Problem with SQL Delete Statement

Joined
Sep 25, 2006
Messages
7
Programming Experience
Beginner
Im new to vb.net. Im trying to set a delete statement to remove a company from a SQL server database. The user can select the line in the datagrid and then choose delete button.

If MsgBox("Are you sure you want to delete this?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Delete") = MsgBoxResult.Yes Then

myCommand = New SqlCommand("DELETE FROM company WHERE companyID=@companyID", myConnection)

ra = myCommand.ExecuteNonQuery()

MessageBox.Show("Records affected: " & ra)

End If

This code deletes the whole company in the database. Can anyone help me?
 
yep its

cmd.parameters.addwithvariable("@companyID", thevalue)
or
cmd.parameters.add("companyID", sqltype.nvarchar).value = thevalue

not completely sure about the last one. i think it might be missing an argument. i dont have a coding environment on this computer to check.

note thevalue is whatever you want it to be,

and im pretty sure a datagrid row cant be deleted, as its used for display only. you need to delete the row from the db, and then update/refresh the datagrid. if you really wanted to you could use listviews, as you can delete rows from there.

hope its helped mate

regards
adam
 
thanx! i'll try that..

this is what i wanna do:

when the users want to delete a record from the database, they can just select the row n then click delete button.

n oh yes i need to know how to update/refresh the datagrid too!
pls plss help me!
ive tried so many ways but it doesnt work..
 
Last edited:
ok, this is the process i think u should follow

1. user presses delete button (after selecting row on datagrid)
2. clear datagrid
3. populate a dataset according to your original query (that filled the datagrid)
4. bind the dataset as the datagrid's datasource.


any questions, let me know mate

regards
adam
 
Dim ds As New DataSet("company")
Dim da As New SqlDataAdapter(myCommand)

myCommand = New SqlCommand("Select * from company", myConnection)

ds.Clear()
da.Fill(ds, "company")
DataGridView1.Refresh()
this is my code for refresh button.
there is an error which is violating primary key ...........
i dunno what's wrong as i didnt put the same value as what is already in the database

thanx
 
this is my code for refresh button.
there is an error which is violating primary key ...........
i dunno what's wrong as i didnt put the same value as what is already in the database

thanx

no probs. just out of curiousity, put the dataadapter line after the sqlcommand line, and then let me know waht happens

regards
adam
 
are you sure you have cleared the previous dataset properly?
before you fill the dataset with the updated data, use the line
thedataset=nothing

to clear the dataset, then re-initialize it and re-fill the dataset.

good luck
regards
adam
 
that means you didnt reinitialize it.

try
'unbind the dataset from the datagrid
'then clear the datagrid

not sure about the code for unbinding/binding cause i dont use datagrids very much

'set the old ds to nothing,

ds=nothing

'then reinitilize it

ds=new dataset

then, re-fill the dataset and re bind it to the datagrid
 
Post your project, without the binaries (choose "Clean Solution" from the build menu before you put your project in a zip file) and I'll fix it, commenting to show you how. I would normally ask you for more information on how you have arranged the data access layer, but I'll be able to determine that more easily for myself if I can see it..
 
Back
Top