2010年8月11日 星期三

passing empty string to SqlCommand.Parameter.Add

ColumnName DataType Length AllowNulls
sampleKey char 10
engDesc char 40 Yes
frDesc char 40 Yes
When I try to add a record,if the Textbox is empty then I am getting an errror. If it's not empty then it adds to the table. But in database, I checked those fields to accept null values. Here is the sample code below.


Dim sqlQuery as String = "INSERT INTO SampleTable(sampleKey,engDesc,frDesc) VALUES (@key,@english,@french)"

Dim cmd As SqlCommand = SqlConnection1.CreateCommand

cmd.Parameters.Add("@key", txtKey.Text)
cmd.Parameters.Add("@english", txtEnglish.Text)
cmd.Parameters.Add("@french", txtFrench.Text)
cmd.CommandType = CommandType.Text
cmd.CommandText = sqlQuery

SqlConnection1.Open()
cmd.ExecuteNonQuery()
SqlConnection1.Close()

沒有留言:

張貼留言