0

I'm making a simple web app like Instagram. I want to store images for each user in separate table, for example:

  1. New users login is Bob123,
  2. Create a new record in table Users (Uzytkownicy) for Bob123,
  3. Create a new table, in the same database, called Bob123 and there store all Bobs images

I'm getting an error: Incorrect syntax near '@name'

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    try
    {
        if (temp == 0)
        {
            Guid newGuid = Guid.NewGuid();

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["UzytkownicyConnectionString"].ConnectionString);
            conn.Open();

            string insertQuery = "insert into Uzytkownicy (Id, Login, Password) values (@id, @Uname, @password)";
            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.AddWithValue("@id", newGuid.ToString());
            com.Parameters.AddWithValue("@Uname", TextBoxUN.Text);
            com.Parameters.AddWithValue("@password", TextBoxPass.Text);

            SqlCommand comm = new SqlCommand("CREATE TABLE @name ([Image] VARBINARY (MAX) NOT NULL);", conn);
            comm.Parameters.AddWithValue("@name", TextBoxUN.Text);

            com.ExecuteNonQuery();
            comm.ExecuteNonQuery();

            Response.Redirect("MainWebSite.aspx");
            conn.Close();
        }
    }
    catch (Exception ex)
    {
        Response.Write("Error: " + ex.ToString());
    }
}
P.Musiał
  • 81
  • 5

3 Answers3

0

@name is incorrect syntax.

You are attempting to execute this specific line of code :

"CREATE TABLE @name ([Image] VARBINARY (MAX) NOT NULL);"

against an sql database. SQL does not know what @name is.

for hacky, but correct, syntax, you should do:

...

string tablename = "somename"; ...

"CREATE TABLE " + tablename + " ([Image] VARBINARY (MAX) NOT NULL);"

Morten Bork
  • 1,413
  • 11
  • 23
0

you caanot create table with the name of "@name" use any other name

CREATE TABLE table_name([Image] VARBINARY (MAX) NOT NULL);", conn);
user7415073
  • 290
  • 4
  • 22
0

I think you meant to write @Uname instead?

As the others say, @name is not a valid tablename.

Try doing something like this:

string tablename = "Bob123";
SqlCommand comm = new SqlCommand(""CREATE TABLE " + tablename + " ([Image] VARBINARY (MAX) NOT NULL);", conn);
thallium
  • 33
  • 1
  • 5