Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert to sql server. But all datas are inserted except the values in the datagridview column(1). Please notify where I did mistake. Please help me. The values in columns(1) always a numerical value like invoice amount.

Button1 click event
******************************************************************
VB
Dim conn As String = ("Data Source=BASHEER-PC;Initial Catalog=comrade;User ID=sa;Password=QDCqdc123")
        Dim Cn As New SqlConnection(conn)
        Dim mycommand As New SqlCommand

        mycommand.CommandType = CommandType.StoredProcedure
        mycommand.CommandText = "Invapp"
        Dim i As Integer
        

        Convert.ToInt32(DataGridView3.Rows(i).Cells(1).Value)

        For i = 0 To DataGridView3.RowCount - 1
            mycommand.Parameters.Add("@Voucher_Type", SqlDbType.NChar).Value = "RV"
            mycommand.Parameters.Add("@voucher_no", SqlDbType.Int).Value = Txtrvno.Text
            mycommand.Parameters.Add("@Account_number", SqlDbType.VarChar).Value = Txtcraccountno.Text
            mycommand.Parameters.Add("@Account_name", SqlDbType.VarChar).Value = Txtcraccountname.Text
            mycommand.Parameters.Add("@Debit_Amount", SqlDbType.Money).Value = 0
            mycommand.Parameters.Add("@Credit_Amount", SqlDbType.Money).Value = 0
            mycommand.Parameters.Add("@Voucher_date", SqlDbType.SmallDateTime).Value = Txtdate.Text
        
            mycommand.Parameters.Add("@Bal_Amount", SqlDbType.NVarChar).Value = DataGridView3.Rows(i).Cells(1).Value
          

            mycommand.Connection = Cn
            Try
                Cn.Open()
                mycommand.ExecuteNonQuery()
                MsgBox("Posted Sucessfully", MsgBoxStyle.Information)
                Cn.Close()

            Catch ex As Exception
                '  Throw ex
            Finally
                Cn.Dispose()
                '   end catch


            End Try
        Next
Posted
Updated 16-Jul-13 2:32am
v5
Comments
Nirav Prabtani 16-Jul-13 8:14am    
try to check by break point ...:)
Basheer Ahammed 16-Jul-13 8:20am    
please help me nirav.I donot get you
Basheer Ahammed 16-Jul-13 8:50am    
there ia a loop 0 to datagridview3.rowscount
Maciej Los 16-Jul-13 9:03am    
Check Stored Procedure!
ZurdoDev 16-Jul-13 9:12am    
You should be able to find it quickly using breakpoints and debugging the code.

SQL
For Each rw As DataGridViewRow In DataGridView3.Rows
            mycommand.Parameters.Add("@Bal_Amount", SqlDbType.NVarChar).Value = rw.Cells(1).Value
        Next
OR 
For Each rw As DataGridViewRow In DataGridView3.Rows
            mycommand.Parameters.Add("@Bal_Amount", SqlDbType.NVarChar).Value = rw.Cells("Amt").Value
        Next


you try this code
 
Share this answer
 
try this
C#
mycommand.Parameters.Add("@Bal_Amount", SqlDbType.NVarChar).Value = DataGridView3.Rows(i).Cells(1).Value.ToString()

and hope you know if it is the first column of your datagridview then you had to use
C#
DataGridView3.Rows(i).Cells(0).Value.ToString()//as index starts with 0
 
Share this answer
 
Comments
Basheer Ahammed 16-Jul-13 10:59am    
i tested as per your code.But result. Same problem.

Nothing has happened
Basmeh Awad 16-Jul-13 15:41pm    
add a break point and debug it till this statement and highlight this
DataGridView3.Rows(i).Cells(1).Value.ToString()
Right click and select "Quick Watch".
see are you getting any value??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900