Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
i write this code to store the text content of a file and the file itself in a database ,
but i have a problem that store the foreign key in text table as a null

C#
public string storeText_intoDB(string str )
{
    using (SqlConnection conn = new SqlConnection("Data Source=TOSHIBA-PC\\SQLEXPRESS;" +
    "Initial Catalog=FileDB;Integrated Security=True;Pooling=False"))
    {
        try
        {
            conn.Open();
            //SELECT MAX(column_name) FROM table_name
            string strsql = " select MAX( FileID )from File_Table  ";
            SqlCommand cmd1 = new SqlCommand(strsql, conn);
            object result = cmd1.ExecuteScalar();

            if (result == null)
            {

                return "Error IN FileID";
            }
            else
            {
                SqlDataReader myreader;
                myreader = cmd1.ExecuteReader();

                while (myreader.Read())
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "StoredProcedure1";
                    cmd.Connection = conn;

                    cmd.Parameters.AddWithValue("@FileID", myreader["FileID"]);

                    cmd.Parameters.AddWithValue("@ttext", str);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Texts STORED successfully");
                    return "text stored Successfully!!!";
                }
                MessageBox.Show("Texts don't STORED successfully");
                return "My reader not working in text stored !";


            }
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
            finally
            {
                conn.Close();

                conn.Dispose();

            }
        }



        }

but still store the fileID as nulls ;\ ???
can anyone help me please???
Posted
Comments
[no name] 23-Feb-13 7:35am    
In which coloumn have forgin key??? in file table
Sara Ahmad 23-Feb-13 8:02am    
forgin key in text table that reference to fileID in file table
[no name] 23-Feb-13 8:00am    
did u insert data in primarykey column!!!?
Sara Ahmad 23-Feb-13 8:03am    
yes i did ,
as this
{
//Data Source=TOSHIBA-PC\SQLEXPRESS;Initial Catalog=FileDB;Integrated Security=True;Pooling=False
using (SqlConnection conn = new SqlConnection("Data Source=TOSHIBA-PC\\SQLEXPRESS;" +
"Initial Catalog=FileDB;Integrated Security=True;Pooling=False"))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "StoredProcedure2";
cmd.Connection = conn;

cmd.Parameters.AddWithValue("@FileName", fileName);
cmd.Parameters.AddWithValue("@Data", data);




try
{
conn.Open();
cmd.ExecuteNonQuery();
return "File stored Successfully!!!";
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
Prakash Thirumoorthy 24-Feb-13 23:03pm    
have u set allow null value for this field?

1 solution

hello dude do you know Sql Syntax's first try yo insert the data using INSERT INTO syntax if it works there is no problem in your database if not check your Index.Index plays main role to insert data in your table.
 
Share this answer
 
v2

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