UPDATE Command problem

Discussion in '.NET' started by sushainc, Sep 26, 2009.

  1. sushainc

    sushainc

    Joined:
    Sep 26, 2009
    Messages:
    1
    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.


    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    sushainc, Sep 26, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BW
    Replies:
    1
    Views:
    772
  2. Mr Newbie

    DataGrid - Update Command Event Problem

    Mr Newbie, Sep 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    529
    Mr Newbie
    Sep 21, 2005
  3. thangchan@gmail.com

    getting problem in asp.net SQL update command

    thangchan@gmail.com, Jan 29, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,247
    Mr Newbie
    Jan 29, 2006
  4. prakash
    Replies:
    2
    Views:
    586
    prakash
    Apr 11, 2005
  5. Stephan Kämper
    Replies:
    1
    Views:
    143
    Assaph Mehr
    Jul 15, 2006
Loading...

Share This Page