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
 Development Tools
 ASP.NET
 Insert Null values using C#

Author  Topic 

mejohnm
Starting Member

1 Post

Posted - 2011-05-22 : 13:22:02
Hello all. I have a program which has 11 variables that need to be inserted to the DB. Some of these can be empty strings which I am having a problem to get inserted into the DB. I appreciate if anyone can help. Here is the code:


private void PostDatatoServer()
{
String connectionString = @"Data Source=LUCKYTIGER\SQLEXPRESS;Initial Catalog=John;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
textBox1.Text = "Connection made";
string str = "";
str += "INSERT INTO Parsed(Date, Gal, Sys, Sl, ST, PN, PlayN, Sym, Rk, All, Rel)";
str += "VALUES(@Date, @Gal, @Sys, @Sl, @ST, @PN, @PlayN, @Sym, @Rk, @All, @Rel)";
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@Date", uegParser.strTime));
cmd.Parameters.Add(new SqlParameter("@Gal", Convert.ToInt16(uegParser.strGalaxyNum)));
cmd.Parameters.Add(new SqlParameter("@Sys", Convert.ToInt16(uegParser.strSystemNum)));
cmd.Parameters.Add(new SqlParameter("@Sl", uegParser.intSlot));
cmd.Parameters.Add(new SqlParameter("@ST", uegParser.intSlotType));
if (uegParser.strPlanetName == "")
cmd.Parameters.Add(new SqlParameter("@PN", SqlDbType.NVarChar).Value = DBNull.Value);
else
cmd.Parameters.Add(new SqlParameter("@PN", uegParser.strPlanetName));
if (uegParser.strPlayerName == "")
{
cmd.Parameters.Add(new SqlParameter("@PlayN", DBNull.Value));
TextBox2.Text = "Null player name";
}
else
{
cmd.Parameters.Add(new SqlParameter("@PlayN", uegParser.strPlayerName));
}
if (uegParser.strSymbols == "")
cmd.Parameters.Add(new SqlParameter("@Sys", DBNull.Value));
else
cmd.Parameters.Add(new SqlParameter("@Sym", uegParser.strSymbols));
if (uegParser.strRank == "")
cmd.Parameters.Add(new SqlParameter("@Rk", DBNull.Value));
else
cmd.Parameters.Add(new SqlParameter("@Rk", uegParser.strRank));
if (uegParser.strAlliance == "")
cmd.Parameters.Add(new SqlParameter("@All", DBNull.Value));
else
cmd.Parameters.Add(new SqlParameter("@All", uegParser.strAlliance));
cmd.Parameters.Add(new SqlParameter("@Rel", uegParser.intRelationship));

cmd.ExecuteNonQuery();
con.Close();
TextBox2.Text = "Connection closed";
}
   

- Advertisement -