Click here to Skip to main content
       

Visual Basic

 
Questionhaving problems with program to merge sort a given set of input Pinmemberbenkazy101415-Oct-12 0:23 
QuestionRe: having problems with program to merge sort a given set of input PinmemberEddy Vluggen15-Oct-12 0:50 
AnswerRe: having problems with program to merge sort a given set of input Pinmemberbenkazy101415-Oct-12 1:36 
QuestionRe: having problems with program to merge sort a given set of input PinmemberEddy Vluggen15-Oct-12 2:16 
AnswerRe: having problems with program to merge sort a given set of input Pinmemberbenkazy101415-Oct-12 2:38 
GeneralRe: having problems with program to merge sort a given set of input PinmemberEddy Vluggen15-Oct-12 2:47 
AnswerRe: having problems with program to merge sort a given set of input Pinmemberbenkazy101415-Oct-12 1:38 
Question[SOLVED] How to update Gridview with value textbox = Null [modified] Pinmemberzaimah14-Oct-12 22:25 
Hi, its been years i don't do vb. So its like im a junior in this. I have problem update my Gridview. My gridview is to update marks for student. The problem is, the update have problem if the textbox is with a NULL value. If i put 0 in the textbox, the update runs ok.
 
This is my gridview html
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
            <Columns> 
                <asp:TemplateField HeaderText="Student ID" SortExpression="stID">
                    <ItemTemplate>
                        <asp:Label ID="Label11" runat="server" Width="50px" Text='<%# Bind("stID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO1" SortExpression="CO1">
                    <ItemTemplate>
                        <asp:TextBox ID="Label1" runat="server" Width="50px" Text='<%# Bind("CO1") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO2" SortExpression="CO2">
                    <ItemTemplate>
                        <asp:TextBox ID="Label2" runat="server" Width="50px" Text='<%# Bind("CO2") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO3" SortExpression="CO3">
                    <ItemTemplate>
                        <asp:TextBox ID="Label3" runat="server" Width="50px" Text='<%# Bind("CO3") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO4" SortExpression="CO4">
                    <ItemTemplate>
                        <asp:TextBox ID="Label4" runat="server" Width="50px" Text='<%# Bind("CO4") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO5" SortExpression="CO5">
                    <ItemTemplate>
                        <asp:TextBox ID="Label5" runat="server" Width="50px" Text='<%# Bind("CO5") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO6" SortExpression="CO6">
                    <ItemTemplate>
                        <asp:TextBox ID="Label6" runat="server" Width="50px" Text='<%# Bind("CO6") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO7" SortExpression="CO7">
                    <ItemTemplate>
                        <asp:TextBox ID="Label7" runat="server" Width="50px" Text='<%# Bind("CO7") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO8" SortExpression="CO8">
                    <ItemTemplate>
                        <asp:TextBox ID="Label8" runat="server" Width="50px" Text='<%# Bind("CO8") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO9" SortExpression="CO9">
                    <ItemTemplate>
                        <asp:TextBox ID="Label9" runat="server" Width="50px" Text='<%# Bind("CO9") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CO10" SortExpression="CO10">
                    <ItemTemplate>
                        <asp:TextBox ID="Label10" runat="server" Width="50px" Text='<%# Bind("CO10") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
             <RowStyle BackColor="White" ForeColor="#330099" />
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <HeaderStyle BorderColor="Maroon" BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:UTMConnectionString %>"
            SelectCommand="SELECT [stID], [CO1], [CO2], [CO3], [CO4], [CO5], [CO6], [CO7], [CO8], [CO9], [CO10] FROM [quiz] WHERE ([Cid] = @Cid)">
            <SelectParameters>
                <asp:SessionParameter Name="Cid" SessionField="CourseID" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
 
The erros is on line:
Quote:
Dim C1 As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text

 
This error will happen if i didn't put any values in the text box.. The error will be:
 
[FormatException: Input string was not in a correct format.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +205
   Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +101
 
[InvalidCastException: Conversion from string "" to type 'Integer' is not valid.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +259
 
My button to update my gridview..
 
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        
 
        Dim rowcollect As GridViewRowCollection = GridView1.Rows
        Dim index As Integer = GridView1.SelectedIndex
        Dim cd As String = Session("CourseID")
        Dim id As String = Session("userID")
        Dim totQuiz, totC1, totC2, totC3, totC4, totC5 As Integer
        Dim totC6, totC7, totC8, totC9, totC10 As Integer
 

        For Each row As GridViewRow In rowcollect
 
            Dim C1 As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text
            Dim C2 As Integer = DirectCast(row.FindControl("Label2"), TextBox).Text
            Dim C3 As Integer = DirectCast(row.FindControl("Label3"), TextBox).Text
            Dim C4 As Integer = DirectCast(row.FindControl("Label4"), TextBox).Text
            Dim C5 As Integer = DirectCast(row.FindControl("Label5"), TextBox).Text
            Dim C6 As Integer = DirectCast(row.FindControl("Label6"), TextBox).Text
            Dim C7 As Integer = DirectCast(row.FindControl("Label7"), TextBox).Text
            Dim C8 As Integer = DirectCast(row.FindControl("Label8"), TextBox).Text
            Dim C9 As Integer = DirectCast(row.FindControl("Label9"), TextBox).Text
            Dim C10 As Integer = DirectCast(row.FindControl("Label10"), TextBox).Text
            Dim sID As String = DirectCast(row.FindControl("Label11"), Label).Text
 

            'CID = GridView1.Rows(index).Cells(1).Text

 
            If row.RowType = DataControlRowType.DataRow Then
 
                If index = -1 Then
                    index = 0
                End If
 

 
                For i As Integer = 1 To row.Cells.Count - 1
 
                    Dim conn As New SqlConnection(ConfigurationManager.AppSettings("strConn"))
                    Dim MySQL As String = "UPDATE quiz SET CO1=@c1,CO2=@c2,CO3=@c3,CO4=@c4,CO5=@c5,CO6=@c6,CO7=@c7,CO8=@c8,CO9=@c9,CO10=@c10 WHERE CourseID=@cID AND stID=@id"
                    Dim cmd As SqlCommand = New SqlCommand(MySQL, conn)
 
                    cmd.Parameters.AddWithValue("@cID", cd.Trim)
                    cmd.Parameters.AddWithValue("@id", sID)
 

                    cmd.Parameters.Add(New SqlParameter("@c1", SqlDbType.Int, 4))
                    cmd.Parameters("@c1").Value = Convert.DBNull(C1)
                    totC1 += C1
 
                   
                    cmd.Parameters.Add(New SqlParameter("@c2", SqlDbType.Int, 4))
                    cmd.Parameters("@c2").Value = C2
                    totC2 += C2
 

                    cmd.Parameters.Add(New SqlParameter("@c3", SqlDbType.Int, 4))
                    cmd.Parameters("@c3").Value = C3
                    totC3 += C3
 
                    cmd.Parameters.Add(New SqlParameter("@c4", SqlDbType.Int, 4))
                    cmd.Parameters("@c4").Value = C4
                    totC4 += C4
 
                    cmd.Parameters.Add(New SqlParameter("@c5", SqlDbType.Int, 4))
                    cmd.Parameters("@c5").Value = C5
                    totC5 += C5
 
                    cmd.Parameters.Add(New SqlParameter("@c6", SqlDbType.Int, 4))
                    cmd.Parameters("@c6").Value = C6
                    totC6 += C6
 
                    cmd.Parameters.Add(New SqlParameter("@c7", SqlDbType.Int, 4))
                    cmd.Parameters("@c7").Value = C7
                    totC7 += C7
 
                    cmd.Parameters.Add(New SqlParameter("@c8", SqlDbType.Int, 4))
                    cmd.Parameters("@c8").Value = C8
                    totC8 += C8
 
                    cmd.Parameters.Add(New SqlParameter("@c9", SqlDbType.Int, 4))
                    cmd.Parameters("@c9").Value = C9
                    totC9 += C9
 
                    cmd.Parameters.Add(New SqlParameter("@c10", SqlDbType.Int, 4))
                    cmd.Parameters("@c10").Value = C10
                    totC10 += C10
 
                    totQuiz = C1 + C2 + C3 + C4 + C5 + C6 + C7 + C8 + C9 + C10
 
                    cmd.Parameters.Add(New SqlParameter("@total", SqlDbType.Int, 4))
                    cmd.Parameters("@total").Value = totQuiz
 

 
                    cmd.Connection.Open()
                    cmd.ExecuteNonQuery()
                    cmd.Connection.Close()
 
                Next
            End If
        Next
 
        totCO(totC1, totC2, totC3, totC4, totC5, totC6, totC7, totC8, totC9, totC10)
 

    End Sub
 
 
I try to put value '0' if the text box = Null, but it didn't work.
Its been few days I've been trying to find answers. Hope someone can suggest any idea for me to solve this.

modified 31-Oct-12 7:11am.

AnswerRe: How to update Gridview with value textbox = Null PinmemberEddy Vluggen15-Oct-12 0:48 
GeneralRe: How to update Gridview with value textbox = Null Pinmemberzaimah15-Oct-12 5:46 
GeneralRe: How to update Gridview with value textbox = Null PinmemberEddy Vluggen15-Oct-12 6:13 
GeneralRe: How to update Gridview with value textbox = Null Pinmemberzaimah15-Oct-12 13:48 
GeneralRe: How to update Gridview with value textbox = Null PinmemberEddy Vluggen16-Oct-12 0:14 
GeneralRe: How to update Gridview with value textbox = Null Pinmemberzaimah16-Oct-12 2:35 
GeneralRe: How to update Gridview with value textbox = Null PinmemberEddy Vluggen16-Oct-12 2:42 
GeneralRe: How to update Gridview with value textbox = Null Pinmemberzaimah16-Oct-12 2:59 
GeneralRe: How to update Gridview with value textbox = Null PinmemberEddy Vluggen16-Oct-12 3:17 
GeneralRe: How to update Gridview with value textbox = Null Pinmemberzaimah16-Oct-12 3:50 
GeneralRe: How to update Gridview with value textbox = Null PinmemberEddy Vluggen16-Oct-12 4:21 
GeneralRe: How to update Gridview with value textbox = Null Pinmemberzaimah17-Oct-12 4:13 
GeneralRe: How to update Gridview with value textbox = Null PinmemberEddy Vluggen17-Oct-12 4:51 
GeneralRe: How to update Gridview with value textbox = Null Pinmemberzaimah17-Oct-12 5:25 
GeneralRe: How to update Gridview with value textbox = Null Pinmemberzaimah17-Oct-12 6:04 
GeneralRe: How to update Gridview with value textbox = Null PinmemberEddy Vluggen17-Oct-12 9:20 
AnswerRe: [SOLVED] How to update Gridview with value textbox = Null PinmemberjosephSurgeon31-Oct-12 4:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 15 Sep 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid