UPDATE Command problem

Joined
Sep 26, 2009
Messages
1
Reaction score
0
My update command was working fine until yesterday but this morning, i don't know what went wrong, it somehow is not reading anything from the TextBox. Each time it would enter the catch saying "error since field is null". PLease help. Below is the snippet of what i've done.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Protected Sub GridView10_update(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)


Dim connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\OutindexISH.mdf;Integrated Security=True;User Instance=True"
Dim myConnection As SqlConnection = New SqlConnection
myConnection.ConnectionString = connString

' create a data adapter
Dim cmd7 As New SqlCommand("select CustodianTable from " + Request.ServerVariables("AUTH_USER") + " where ProjectName = '" + sname.Text + "'", myConnection)
myConnection.Open()
Dim dr7 As SqlDataReader = cmd7.ExecuteReader
Dim table_name7 As String
table_name7 = "temp"
If (dr7.Read()) Then
table_name7 = dr7("CustodianTable")
End If

dr7.Close()
myConnection.Close()


Dim CID1 As String
CID1 = GridView10.DataKeys(e.RowIndex).Value
MsgBox(CID1)


Dim SqlCommand As New SqlCommand("UPDATE " + table_name7 + " SET FirstName = @FirstName, LastName = @LastName, Company = @Company, ContactNumber = @ContactNumber, ContactEmailAddress = @ContactEmailAddress, City = @City, State = @State, Collectdate = @Collectdate, Status = @Status WHERE CID = '" + CID1 + "'", myConnection)


SqlCommand.Parameters.Add("@FirstName", SqlDbType.VarChar)
SqlCommand.Parameters.Add("@LastName", SqlDbType.VarChar)
SqlCommand.Parameters.Add("@Company", SqlDbType.VarChar)
SqlCommand.Parameters.Add("@ContactNumber", SqlDbType.VarChar)
SqlCommand.Parameters.Add("@ContactEmailAddress", SqlDbType.VarChar)
SqlCommand.Parameters.Add("@City", SqlDbType.VarChar)
SqlCommand.Parameters.Add("State", SqlDbType.VarChar)
SqlCommand.Parameters.Add("CollectDate", SqlDbType.VarChar)
SqlCommand.Parameters.Add("@Status", SqlDbType.VarChar)

Dim cols() As String = {"@FirstName", "@LastName", "@Company", "@ContactNumber", "@ContactEmailAddress", "@City", "State", "CollectDate", "@Status"}

Dim Str As String = CType(GridView10.Rows(e.RowIndex).Cells(3).Controls(1), TextBox).Text

MsgBox(Str)

Dim i As Integer

Dim colvalue As String
For i = 0 To 8
MsgBox(i)
colvalue = CType(GridView10.Rows(e.RowIndex).Cells(i + 3).Controls(1), TextBox).Text // i+3 since in my gridview first three columns are checkbox, serialnumber and viewdetail


MsgBox(colvalue) //won't take anything from the textboxes

If (i < 9 And colvalue = "") Then
MsgBox("ERROR: Null values not allowed")
Exit Sub
End If

SqlCommand.Parameters(cols(i)).Value = colvalue
Next i



myConnection.Open()

' MsgBox("hi")
SqlCommand.ExecuteNonQuery()
' GridView10.EditIndex = -1

Dim Faltu2 As String
Faltu2 = "CID"
If Page.IsPostBack Then
BindData(Faltu2)
End If

myConnection.Close()


End Sub
-------------------------------------------------------------------------------.aspx----------------------------------------------------------------------------------------

<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "FirstName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID = "txtbox1" Width="80%">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>


This is how other columns are defined.


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top