Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a sql server database in godaddy and created a table named property manually.i also successfuly connected my application to the database using connection string.But i am unable to insert any values to the table using my c# code Below is my C# code

string strQuery = "INSERT INTO property(name,email,phone,heading,description,location,image1,image2,image3,image4) VALUES('" + name + "','" + email + "','" + phone + "','" + title + "','" + description + "','" + district + "',@data,@data2,@data3,@data4);";
SqlCommand cmd = new SqlCommand(strQuery);


cmd.Parameters.Add("@data", SqlDbType.Binary).Value = bytes;
cmd.Parameters.Add("@data2", SqlDbType.Binary).Value = bytes2;
cmd.Parameters.Add("@data3", SqlDbType.Binary).Value = bytes3;
cmd.Parameters.Add("@data4", SqlDbType.Binary).Value = bytes4;
SqlConnection con = new SqlConnection(constr);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
    con.Open();
    cmd.ExecuteNonQuery();
    return true;
}
catch (Exception ex)
{
    Response.Write(ex.Message);
    return false;
}
finally
{
    con.Close();
    con.Dispose();
}


when i tried to insert value in sql server web admin, i am getting error-Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
Please help me
Posted
Updated 6-Jan-14 4:04am
v2

First off, stop mixing styles and use parametrized queries throughout: concatenating strings is wide open to SQL injection attack which could damage or destroy your database. This may sovle your problem anyway.
Second, look at any error message you get via the catch - it should give you a clue as to what is wrong.

At the moment, there are far too many possibilities for error here, that we can't even begin to guess which one of they you have caused without better information.

C#
string strQuery = "INSERT INTO property(name, email, phone, heading, description, location, image1, image2, image3, image4) VALUES(@NM, @EM, @PN, @HD, @DS, @LO, @D1, @D2, @D3, @D4)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@NM", name);
cmd.Parameters.AddWithValue("@EM", email);
cmd.Parameters.AddWithValue("@PN", phone);
cmd.Parameters.AddWithValue("@HD", title);
cmd.Parameters.AddWithValue("@DS", description);
cmd.Parameters.AddWithValue("@LO", district);
cmd.Parameters.AddWithValue("@D1", bytes);
cmd.Parameters.AddWithValue("@D2", bytes2);
cmd.Parameters.AddWithValue("@D3", bytes3);
cmd.Parameters.AddWithValue("@D4", bytes4);
SqlConnection con = new SqlConnection(constr, con);
try
   {
   ...
 
Share this answer
 
Comments
faizel s 4-Jan-14 7:49am    
i tried this but still not working.it does'nt show any error.but when i load contents of databse onto datatable after inserting it says zero row for datatable
OriginalGriff 4-Jan-14 7:52am    
If you didn't get any error, then they problem is more likely to be with how you load the datatable from the database - show the code you use for that as well.
faizel s 4-Jan-14 7:59am    
DataTable dt = new DataTable();

// SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\work\boatsite - Copy\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
string query = "SELECT * FROM property WHERE id = (SELECT MAX(id) FROM property);";
if (cn.State == ConnectionState.Closed)
cn.Open();
SqlCommand cmd = new SqlCommand(query, cn);

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
cn.Close();
faizel s 4-Jan-14 7:53am    
DataTable dt = new DataTable();

// SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\work\boatsite - Copy\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
string query = "SELECT * FROM property WHERE id = (SELECT MAX(id) FROM property);";
if (cn.State == ConnectionState.Closed)
cn.Open();
SqlCommand cmd = new SqlCommand(query, cn);

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
cn.Close();
faizel s 4-Jan-14 7:55am    
Thnk you for trying to help me.Coding i wrote ws perfectly working in local server
Try with below link, might be an issue with the connection string you specified.
Link
 
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