hi can anyone tell me why my adding record command does not work,,I also think it might have to do with adding permissions in sql server ,,but I"m not sure ,plz help
dim myconnection As sqlconnection
sub page_load(sender as object,E as EventArgs)
myconnection = new sqlconnection("server= G103-TT03;database=CampusLANDB;Trusted_Connection=true")
If Not (IsPostBack)
DataBind()
end if
end sub
Sub addNics_Click(sender as object,E as EventArgs)
Dim Ds as dataSet
Dim mycommand as sqlcommand
if au_MAC_Address.value = "" or au_Name.value="" or au_Actiontext.value="(NONE)" or au_ExpirationDate.value=""
Message.InnerHtml="Error:Null Values not Allowed for Nic_Name ,MAC_Address "
Message.style("color")="red"
DataBind()
end if
Dim InsertCmd As string ="Insert Into tNics (au_FK_Students,au_Name,au_MAC_Address,au_actiontext)values(@FK_Students,@Name,@MAC_Address,@actiontext)"
mycommand =new sqlcommand (insertCmd , myconnection)
mycommand.parameters.add(new sqlparameter("@FK_Students", sqlDBType.Int, 4))
mycommand.parameters("@FK_Students").value = server.htmlencode(au_FK_Students.value)
mycommand.parameters.add(new sqlparameter("@Name", sqlDBtype.NVarChar,50))
mycommand.parameters("@Name").value=server.htmlEncode(au_Name.value)
mycommand.parameters.add(new sqlparameter("@MAC_Address",SqlDBType.NvarChar,12))
mycommand.parameters("@MAC_Address").Value=server.htmlEncode(au_MAC_Address.Value)
mycommand.parameters.add(new sqlparameter("@actiontext",sqlDBType.NvarChar,20))
mycommand.parameters("@actiontext").value= server.HtmlEncode(au_actiontext.value)
mycommand.parameters.add(new sqlparameter("@expirationdate", sqldbType.NvarChar,20))
mycommand.parameters("@expirationdate").value = server.htmlEncode(au_expirationdate.value)
mycommand.connection.open()
try
mycommand.ExecuteNonQuery()
Message.InnerHtml=" record Added
"& InsertCmd.ToString()
Catch Exp as SQLException
if Exp.Number=2627
Message.Innerhtml="Error:A record Already exist With the same Identity"
else
Message.Innerhtml="Error could not add record,please insure that there are no null values"
end if
Message.Style("color")="red"
End try
mycommand.connection.close()
dataBind()
end sub
sub dataBind()
dim mycommand as sqlDataAdapter= new sqldataAdapter("select id_students,Name,EventDate,actiontext,ID_Action,mac_address,expirationdate from tNics,tAction,tStudents,tHistory Where tstudents.iD_Students=tNics.FK_Students and tHistory.FK_Action=tAction.ID_Action and tstudents.login1='pmkosana'", myconnection)
dim DS as dataset= new dataset()
mycommand.fill(DS,"tNics,tStudents,tAction,tHistory")
mydatagrid.dataSource=DS.tables("tNics,tStudents,tAction,tHistory").DefaultView
Mydatagrid.dataBind()
end sub
pmkosana
Member
245 Points
49 Posts
Adding Record sample error
Feb 09, 2004 10:02 AM|LINK
dim myconnection As sqlconnection sub page_load(sender as object,E as EventArgs) myconnection = new sqlconnection("server= G103-TT03;database=CampusLANDB;Trusted_Connection=true") If Not (IsPostBack) DataBind() end if end sub Sub addNics_Click(sender as object,E as EventArgs) Dim Ds as dataSet Dim mycommand as sqlcommand if au_MAC_Address.value = "" or au_Name.value="" or au_Actiontext.value="(NONE)" or au_ExpirationDate.value="" Message.InnerHtml="Error:Null Values not Allowed for Nic_Name ,MAC_Address " Message.style("color")="red" DataBind() end if Dim InsertCmd As string ="Insert Into tNics (au_FK_Students,au_Name,au_MAC_Address,au_actiontext)values(@FK_Students,@Name,@MAC_Address,@actiontext)" mycommand =new sqlcommand (insertCmd , myconnection) mycommand.parameters.add(new sqlparameter("@FK_Students", sqlDBType.Int, 4)) mycommand.parameters("@FK_Students").value = server.htmlencode(au_FK_Students.value) mycommand.parameters.add(new sqlparameter("@Name", sqlDBtype.NVarChar,50)) mycommand.parameters("@Name").value=server.htmlEncode(au_Name.value) mycommand.parameters.add(new sqlparameter("@MAC_Address",SqlDBType.NvarChar,12)) mycommand.parameters("@MAC_Address").Value=server.htmlEncode(au_MAC_Address.Value) mycommand.parameters.add(new sqlparameter("@actiontext",sqlDBType.NvarChar,20)) mycommand.parameters("@actiontext").value= server.HtmlEncode(au_actiontext.value) mycommand.parameters.add(new sqlparameter("@expirationdate", sqldbType.NvarChar,20)) mycommand.parameters("@expirationdate").value = server.htmlEncode(au_expirationdate.value) mycommand.connection.open() try mycommand.ExecuteNonQuery() Message.InnerHtml=" record Added "& InsertCmd.ToString() Catch Exp as SQLException if Exp.Number=2627 Message.Innerhtml="Error:A record Already exist With the same Identity" else Message.Innerhtml="Error could not add record,please insure that there are no null values" end if Message.Style("color")="red" End try mycommand.connection.close() dataBind() end sub sub dataBind() dim mycommand as sqlDataAdapter= new sqldataAdapter("select id_students,Name,EventDate,actiontext,ID_Action,mac_address,expirationdate from tNics,tAction,tStudents,tHistory Where tstudents.iD_Students=tNics.FK_Students and tHistory.FK_Action=tAction.ID_Action and tstudents.login1='pmkosana'", myconnection) dim DS as dataset= new dataset() mycommand.fill(DS,"tNics,tStudents,tAction,tHistory") mydatagrid.dataSource=DS.tables("tNics,tStudents,tAction,tHistory").DefaultView Mydatagrid.dataBind() end subthanks in advance ppm