Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Conversion from type 'DBNull' to type 'String' is

Author  Topic 

supriyasinha
Starting Member

1 Post

Posted - 2011-07-12 : 05:46:03
it dsplyng error "Conversion from type 'DBNull' to type 'String' is not valid. "
its not updating my database when it is empty but if value is thier it get updated...
shows error in line


cmd4.Connection = cnn4
cmd4.CommandText = "Select distinct(ch_ro) from name_list1 where region='" & ddlRegion.SelectedValue & "' and country='" & ddlCountry.SelectedValue & "' and site='" & ddlSite.SelectedValue & "'"
cnn4.Open()
TextBox8.Text = cmd4.ExecuteScalar()
cnn4.Close()



and my update command is


str2 = " update name_list1 set " & _
"ch_it_mnr='" & TextBox4.Text & "',ch_ldba='" & TextBox5.Text & "',ch_rdba='" & TextBox7.Text & "',ch_ro='" & TextBox8.Text & "'," & _
"ch_gdba='" & TextBox9.Text & "',ch_gldba='" & TextBox10.Text & "',ch_lo='" & TextBox11.Text & "',ch_la='" & TextBox12.Text & "',ch_soc='" & TextBox13.Text & "'," & _
"snc_it_mnr='" & TextBox14.Text & "',snc_ldba='" & TextBox15.Text & "',snc_rdba='" & TextBox16.Text & "',snc_ro='" & TextBox17.Text & "',snc_gdba='" & TextBox18.Text & "'," & _
"snc_gldba='" & TextBox19.Text & "',snc_lo='" & TextBox20.Text & "',snc_la='" & TextBox21.Text & "',snc_soc='" & TextBox22.Text & "' where region='" & ddlRegion.SelectedValue & "' and country='" & ddlCountry.SelectedValue & "' and site='" & ddlSite.SelectedValue & "'"

cmd2 = New SqlCommand(str2, conn)
conn.Open()
cmd2.ExecuteNonQuery()
conn.Close()


MsgBox("Update successfully")



please help me its urgent....
before their was no error nd it was workng pefctly but not now, dn't knw hw to solve this..

thanks
supriya

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-12 : 07:04:15
This is because you are not getting any result from your first query - the "cmd4.ExecuteScalar()". Change your code to

....
cnn4.Open()
object o = cmd4.ExecuteScalar();
if (o != DBNull.Value)
{
TextBox8.Text = o.ToString();
}
else
{
TextBox8.Text = string.Empty;
}
cnn4.Close()
I have written it in C#, if you are using VB, you will need to change the syntax appropriately. Of course, whether that is the correct logic or not, I don't know.
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2011-07-12 : 09:14:26
Directly concatenating strings in this manner to create your SQL leaves you open to SQL Injection attacks. Please read more about creating parameterized queries and how to avoid SQL injection attacks. https://www.owasp.org/index.php/Top_10_2010-A1

OS
Go to Top of Page
   

- Advertisement -