Princy
Princy

Reputation: 41

"identifier expected". I'm trying to submit this form on ASP.NET C#

The error only occurrs with email, phone and messgae - why is it so? The datatypes used are char for name, int for phone and varchar for Messgae and Email.

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection cnn = new SqlConnection();
    ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
    cnn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select * from ContactUs";
    cmd.Connection = cnn;
    SqlDataAdapter adp = new SqlDataAdapter();
    DataSet ds = new DataSet();
    adp.Fill(ds, "ContactUs");
    SqlCommandBuilder cb = new SqlCommandBuilder(adp);
    DataRow drow = ds.Tables["ContactUs"].NewRow();
    drow["Name"] = TextBox1.Text;         
    drow.["Email"]=TextBox2.Text;
    drow.["Phone"]=TextBox3.Text;
    drow.["Messgae"]=TextBox4.Text;

    ds.Tables["ContactUS"].Rows.Add(drow);
    adp.Update(ds,"ContactUs");
}

Upvotes: 1

Views: 774

Answers (3)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : You are adding dot . while accessing/setting the value into datarow .

Solution : You don't need to add dot . while accessing datarow.

Replace This:

    drow.["Email"]=TextBox2.Text;
        ^^ //remove this
    drow.["Phone"]=TextBox3.Text;
        ^^ //remove this
    drow.["Messgae"]=TextBox4.Text;
        ^^ //remove this

With This:

    drow["Email"]=TextBox2.Text;
    drow["Phone"]=TextBox3.Text;
    drow["Messgae"]=TextBox4.Text;

Upvotes: 1

Iraj
Iraj

Reputation: 1522

add this code :

s.Tables["ContactUs"].Columns.Add("Name", typeof(string));
s.Tables["ContactUs"].Columns.Add("Email", typeof(string));
s.Tables["ContactUs"].Columns.Add("Phone", typeof(int));
s.Tables["ContactUs"].Columns.Add("Messgae", typeof(string));

Upvotes: 0

PMF
PMF

Reputation: 17185

Are your column names correct? There's at least a spelling error (Messgae instead of Message).

Additionally, an array subscript or an indexer is not preceeded by a point. Remove the . between drow and [.

Upvotes: 0

Related Questions