Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

Sql Query

SQL
create table products(product nvarchar(max),
price numeric(18,2),
discount numeric(18,2) default 0)


SQL
CREATE PROCEDURE sp1

@p1 NVARCHAR(MAX),

@p2 NUMERIC(18,2),

@p3 NUMERIC(18,2) default 0
AS
BEGIN
INSERT INTO products  VALUES (@p1 ,@p2 ,@p3 )
End
GO


coding

VB
com = New SqlCommand("sp1", con)
        com.CommandType = CommandType.StoredProcedure
        Try
            com.Parameters.AddWithValue("@p1", TextBox1.Text)
            com.Parameters.AddWithValue("@p2", TextBox2.Text)
            com.Parameters.AddWithValue("@p3", TextBox3.Text)
            com.ExecuteNonQuery()
            MsgBox("Record Added....")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


it shows Type Conversion Error when i insert empty textbox3.....

Advance Thanks
S.Priyan


[edit]added code block[/edit]
Posted
Updated 11-Apr-11 0:28am
v3
Comments
Rakesh From Patna 11-Apr-11 6:03am    
Check the question you create Stored procedure sp2 and execute the sp1

Just do the following;
...
    com.Parameters.AddWithValue("@p2", (TextBox2.Text.Trim().Length == 0 ? "0" : TextBox2.Text))
    com.Parameters.AddWithValue("@p3", (TextBox3.Text.Trim().Length == 0 ? "0" : TextBox3.Text))
...
 
Share this answer
 
You use the numeric, int etc datatype then you want to save empty text then give error sqlserver. so if you require the empty text save then you change the text value is 0(or default value) then save.
 
Share this answer
 
u are trying to insert an empty value into a numeric field so it fails so insert a zero whenever the field is empty...
 
Share this answer
 
For the numeric fields, you have to check id there's data in the text box. If there isn't you should add a NULL value into the database. For adding the null, use System.DBNull.Value[^].
 
Share this answer
 

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